#P15939. [JOI Final 2026] 传奇团子吃家 / Legendary Dango Eater

    ID: 17943 远端评测题 2500ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树倍增并查集JOI(日本)2026

[JOI Final 2026] 传奇团子吃家 / Legendary Dango Eater

Problem Description

Bitaro bought a long skewer of dumplings as a snack. This skewer can be represented by NN positive integers A1,A2,,ANA_1, A_2, \cdots, A_N as follows. For each integer ii with 0iN0 \leq i \leq N, let si=A1+A2++Ais_i = A_1 + A_2 + \cdots + A_i. In particular, we define s0=0s_0 = 0.

  • The skewer consists of sNs_N dumplings arranged in a single column from top to bottom.
  • Each dumpling is either sweet or spicy. The taste of each dumpling can be described as follows.
    • If ii is an odd integer satisfying 1iN1 \leq i \leq N, then the dumplings from the (si1+1)(s_{i-1} + 1)-th to the sis_i-th from the top are sweet.
    • If ii is an even integer satisfying 1iN1 \leq i \leq N, then the dumplings from the (si1+1)(s_{i-1} + 1)-th to the sis_i-th from the top are spicy.

When eating this skewer of dumplings, Bitaro made QQ possible plans. The jj-th plan (1jQ1 \leq j \leq Q) is represented by integers LjL_j and RjR_j satisfying 1LjRjN1 \leq L_j \leq R_j \leq N, and in this plan he eats the dumplings from the (sLj1+1)(s_{L_j-1} + 1)-th from the top through the sRjs_{R_j}-th from the top.

Also, when eating this skewer, Bitaro decided to divide it into several bites in the following manner. Here, KK is a positive integer representing Bitaro’s preference for sweetness.

  • He eats the dumplings from top to bottom, and each dumpling is eaten exactly once.
  • In one bite, he may eat any positive number of consecutive dumplings on the skewer. If, among the dumplings eaten in that bite, the number of sweet dumplings minus the number of spicy dumplings is at least KK, then Bitaro is pleased.

Given the information about the skewer of dumplings and the plans, write a program that, for each plan, computes the maximum possible number of times Bitaro can be pleased.

Input Format

Read the following data from the standard input.

NN QQ KK
A1A_1 A2A_2 \cdots ANA_N
L1L_1 R1R_1
L2L_2 R2R_2
\vdots
LQL_Q RQR_Q

Output Format

Write QQ lines to the standard output. In the jj-th line (1jQ1 \leq j \leq Q), output the maximum number of times Bitaro can be pleased in the jj-th plan.

5 2 1
2 1 2 4 3
1 5
2 4
7
2
5 2 3
2 1 2 4 3
1 5
2 4
2
0
9 4 50
24 26 89 45 84 72 15 31 66
1 9
2 8
4 6
5 6
3
2
1
1

Hint

Sample 1

For the first plan, Bitaro eats the dumplings from the first through the 1212th from the top. By repeatedly eating exactly one dumpling per bite from the top, Bitaro can be pleased 77 times. Since it is impossible to make him pleased 88 times or more, you should output 77.

For the second plan, Bitaro eats the dumplings from the third through the 99th from the top. By repeatedly eating exactly one dumpling per bite from the top, Bitaro can be pleased 22 times. Since it is impossible to make him pleased 33 times or more, you should output 22.

This sample input satisfies the constraints of all subtasks.

Sample 2

This differs from Sample Input 11 only in the value of KK. For the first plan, Bitaro can be pleased 22 times by eating the dumplings in the following four bites.

  • In the first bite, he eats the first through the 55th dumplings from the top. Since the number of sweet dumplings is 44 and the number of spicy dumplings is 11, Bitaro is pleased.
  • In the second bite, he eats only the 66th dumpling from the top. Since the number of sweet dumplings is 00 and the number of spicy dumplings is 11, Bitaro is not pleased.
  • In the third bite, he eats the 77th through the 99th dumplings from the top. Since the number of sweet dumplings is 00 and the number of spicy dumplings is 33, Bitaro is not pleased.
  • In the fourth bite, he eats the 1010th through the 1212th dumplings from the top. Since the number of sweet dumplings is 33 and the number of spicy dumplings is 00, Bitaro is pleased.

Since it is impossible to make him pleased 33 times or more, you should output 22. For the second plan, it is impossible for Bitaro to eat the dumplings in such a way that he is pleased at least once, so you should output 00.

This sample input satisfies the constraints of subtasks 1,3,4,5,61,3,4,5,6.

Sample 3

This sample input satisfies the constraints of subtasks 1,4,5,61,4,5,6.

Constraints

  • 1N5000001 \leq N \leq 500000.
  • 1Q5000001 \leq Q \leq 500000.
  • 1K10141 \leq K \leq 10^{14}.
  • 1Ai1091 \leq A_i \leq 10^9 (1iN1 \leq i \leq N).
  • 1LjRjN1 \leq L_j \leq R_j \leq N (1jQ1 \leq j \leq Q).
  • Given values are all integers.

Subtasks

  1. (6 points) Q10Q \leq 10.
  2. (5 points) K2K \leq 2.
  3. (18 points) K10K \leq 10.
  4. (27 points) A1+A2++AN500000A_1 + A_2 + \cdots + A_N \leq 500\,000.
  5. (17 points) N200000N \leq 200\,000, Q200000Q \leq 200\,000.
  6. (27 points) No additional constraints.