시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB109484145.055%

문제

JOI-kun is breeding $N$ fish in a large tank, and each fish is numbered from $1$ to $N$.

JOI-kun has two types of fish food, A and B, both in sufficient quantities. When one piece of food is added to the aquarium, exactly one fish eats it (any fish could potentially eat it), and depending on the type of food and which fish ate it, the intelligence of the fish changes as follows:

  • When fish $k$ ($1 ≤ k ≤ N$) eats one piece of type A food, the intelligence of fish $k$ increases exactly by $D$.
  • When fish $k$ ($1 ≤ k ≤ N$) eats one piece of type B food, the intelligence of all fish numbered $k$ and above increases exactly by $1$ each.

Currently, the intelligence of all fish is $0$. JOI-kun wants to make the intelligence of fish $i$ ($1 ≤ i ≤ N$) equal to its ideal intelligence $C_i$, but it may not always be possible.

Therefore, he considered $Q$ questions. The $j$-th question ($1 ≤ j ≤ Q$) is as follows:

  • Starting from the state where all fish have an intelligence of $0$, by repeating the action of putting food into the aquarium zero or more times, is it possible to reach the state where all fish $L_j , L_j + 1, \dots , R_j$ have their exact ideal intelligence values? Furthermore, if it is possible, what is the minimum number of pieces of type A food that needs to be put into the tank?

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

입력

Read the following data from the standard input.

$N$ $D$

$C_1$ $C_2$ $\cdots$ $C_N$

$Q$

$L_1$ $R_1$

$L_2$ $R_2$

$\vdots$

$L_Q$ $R_Q$

출력

Write $Q$ lines to the standard input. In the $j$-th line ($1 ≤ j ≤ Q$), if it is possible to reach the state where all fish $L_j , L_j + 1, \dots , R_j$ have their exact ideal intelligence values, output the minimum number of pieces of type A food that needs to be put into the tank. Otherwise, output -1.

제한

  • $1 ≤ N ≤ 300\, 000$.
  • $1 ≤ Q ≤ 300\, 000$.
  • $1 ≤ D ≤ 10^{12}$.
  • $0 ≤ C_i ≤ 10^{12}$ ($1 ≤ i ≤ N$).
  • $1 ≤ L_j ≤ R_j ≤ N$ ($1 ≤ j ≤ Q$).
  • Given values are all integers.

서브태스크

번호배점제한
19

$N ≤ 3\, 000$, $Q ≤ 3\, 000$.

27

$C_i ≤ 1$ ($1 ≤ i ≤ N$).

328

$D = 1$.

420

$C_i ≥ C_{i+1}$ ($1 ≤ i ≤ N - 1$).

536

No additional constraints.

예제 입력 1

4 2
3 1 2 1
1
1 3

예제 출력 1

1

For example, in the following case, all fish $1$, $2$, $3$ eventually have their exact ideal intelligence values, and the number of pieces of type A food put into the tank is $1$.

  • At first, the intelligence of fish $1$, $2$, $3$, $4$ is $0$, $0$, $0$, $0$ respectively.
  • Next, JOI-kun puts one piece of type B food into the tank, which fish $3$ eats. As a result, the intelligence of fish $1$, $2$, $3$, $4$ becomes $0$, $0$, $1$, $1$ respectively.
  • Next, JOI-kun puts one piece of type A food into the tank, which fish $1$ eats. As a result, the intelligence of fish $1$, $2$, $3$, $4$ becomes $2$, $0$, $1$, $1$ respectively.
  • Finally, JOI-kun puts one piece of type B food into the tank, which fish $1$ eats. As a result, the intelligence of fish $1$, $2$, $3$, $4$ becomes $3$, $1$, $2$, $2$ respectively.

Since it is impossible to reach the state where all fish $1$, $2$, $3$ have the exact ideal intelligence values without putting any piece of type A food, output $1$.

This sample input satisfies the constraints of Subtasks 1, 5.

예제 입력 2

4 2
0 1 0 1
3
1 2
2 3
1 1

예제 출력 2

0
-1
0

This sample input satisfies the constraints of Subtasks 1, 2, 5.

예제 입력 3

5 1
3 1 4 1 5
3
1 5
2 4
3 5

예제 출력 3

5
3
3

This sample input satisfies the constraints of Subtasks 1, 3, 5.

예제 입력 4

6 3
16 14 13 8 6 5
4
1 4
2 5
3 3
1 6

예제 출력 4

9
8
0
-1

This sample input satisfies the constraints of Subtasks 1, 4, 5.

첨부

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.