I want u to act as an IOI gold Medalist, u should think very hard about this problem,…
- User
I want u to act as an IOI gold Medalist, u should think very hard about this problem, and solve it using this template #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { cin.tie(0); ios_base::sync_with_stdio(false); ///
}
there should be no comments in the code, but u will explain ur code and heuristics and intuition on how u think of it, and tricks on to use for other problems, all in a nic format that doesnt look bad and weird lol
here is the problem:
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 Ci , 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 Lj , Lj + 1, . . . , Rj 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.
input N D C_1 C_2 ... C_N Q L_1 R_1 L_2 R_2 . . . L_Q R_Q
Output 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 Lj , Lj + 1, . . . , Rj 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. Constraints • 1 ≤ N ≤ 300 000. • 1 ≤ Q ≤ 300 000. • 1 ≤ D ≤ 1012 . • 0 ≤ Ci ≤ 1012 (1 ≤ i ≤ N). • 1 ≤ Lj ≤ Rj ≤ N (1 ≤ j ≤ Q). • Given values are all integers. Subtasks
- (9 points) N ≤ 3 000, Q ≤ 3 000.
- (7 points) Ci ≤ 1 (1 ≤ i ≤ N).
- (28 points) D = 1.
- (20 points) Ci ≥ Ci+1 (1 ≤ i ≤ N − 1).
- (36 points) No additional constraints
SAMPLE INPUT AND OUTPUT sample input 1 4 2 3 1 2 1 1 1 3 sample output 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
- Assistant