Logo
(追記) (追記ここまで)

31726번 - Fish 3 서브태스크다국어

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

문제

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.

힌트

첨부

출처

Camp > JOI Spring Training Camp > JOI 2023/2024 Spring Training Camp 1-1번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /