#P10430. [JOIST 2024] 鱼 3 / Fish 3

[JOIST 2024] 鱼 3 / Fish 3

Problem Description

JOI is raising NN fish in a large tank. The fish are numbered from 11 to NN.

JOI has two types of fish food, AA and BB, and there is an unlimited supply of both. Each time one piece of food is added to the tank, exactly one fish eats it (any fish may eat it). Depending on the type of food and which fish eats it, the fishes’ intelligence changes as follows:

  • When the kk-th fish (1kN1 \leq k \leq N) eats one piece of type AA food, the intelligence of the kk-th fish increases by exactly DD.
  • When the kk-th fish (1kN1 \leq k \leq N) eats one piece of type BB food, the intelligence of all fish whose indices are at least kk increases by exactly 11.

Currently, the intelligence of all fish is 00. JOI wants the intelligence of the ii-th fish (1iN1 \leq i \leq N) to become its desired intelligence CiC_i, but this is not always possible.

Therefore, he considers QQ questions. The jj-th question (1jQ1 \leq j \leq Q) is as follows:

  • Starting from the state where the intelligence of all fish is 00, by repeating the action of putting food into the tank zero or more times, is it possible to reach a state where all fish Lj,Lj+1,,RjL_j, L_j + 1, \ldots, R_j have exactly their desired intelligence values? Moreover, if it is possible, what is the minimum number of type AA food pieces that must be put into the tank?

Write a program that, given information about JOI’s fish and the questions, answers the questions.

Input Format

Read the following from standard input:

  • N,DN, D
  • C1,C2,,CNC_1, C_2, \ldots, C_N
  • QQ
  • L1,R1L_1, R_1
  • L2,R2L_2, R_2
  • ...
  • LQ,RQL_Q, R_Q

Output Format

Output QQ lines. On the jj-th line (1jQ1 \leq j \leq Q), if it is possible to reach a state where fish Lj,Lj+1,,RjL_j, L_j + 1, \ldots, R_j have exactly their desired intelligence values, output the minimum number of type AA food pieces that must be put into the tank. Otherwise, output 1-1.

4 2
3 1 2 1
1
1 3
1
4 2
0 1 0 1
3
1 2
2 3
1 1
0
-1
0
5 1
3 1 4 1 5
3
1 5
2 4
3 5
5
3
3
6 3
16 14 13 8 6 5
4
1 4
2 5
3 3
1 6
9
8
0
-1

Hint

Sample Explanation 1

For example, in the following case, fish 1,2,31, 2, 3 all end up with exactly their desired intelligence values, and the number of type AA food pieces put into the tank is 11.

  • At first, the intelligence of fish 1,2,3,41, 2, 3, 4 is 0,0,0,00, 0, 0, 0, respectively.
  • Next, JOI puts one piece of type BB food into the tank, and it is eaten by fish 33. As a result, the intelligence of fish 1,2,3,41, 2, 3, 4 becomes 0,0,1,10, 0, 1, 1, respectively.
  • Then, JOI puts one piece of type AA food into the tank, and it is eaten by fish 11. As a result, the intelligence of fish 1,2,3,41, 2, 3, 4 becomes 2,0,1,12, 0, 1, 1, respectively.
  • Finally, JOI puts one piece of type BB food into the tank, and it is eaten by fish 11. As a result, the intelligence of fish 1,2,3,41, 2, 3, 4 becomes 3,1,2,23, 1, 2, 2, respectively.
  • Since it is impossible to reach a state where fish 1,2,31, 2, 3 have exactly their desired intelligence values without putting any type AA food, output 11.

This sample satisfies the constraints of Subtasks 11 and 55.

Constraints

  • 1N300,0001 \leq N \leq 300,000.
  • 1Q300,0001 \leq Q \leq 300,000.
  • 1D10121 \leq D \leq 10^{12}.
  • 0Ci10120 \leq C_i \leq 10^{12} (1iN1 \leq i \leq N).
  • 1LjRjN1 \leq L_j \leq R_j \leq N (1jQ1 \leq j \leq Q).
  • All given values are integers.

Subtasks

  • (9 points) N3,000N \leq 3,000, Q3,000Q \leq 3,000.
  • (7 points) Ci1C_i \leq 1 (1iN1 \leq i \leq N).
  • (28 points) D=1D = 1.
  • (20 points) CiCi+1C_i \geq C_{i+1} (1iN11 \leq i \leq N - 1).
  • (36 points) No additional constraints.

Translated by ChatGPT 5