#include<bits/stdc++.h>
using namespace std;
const long long maxn=300000+10;
long long n,q,d,all[maxn],res[maxn],suma[maxn],kaf=(1<<19);
vector<pair<long long,long long>>qu[maxn];
vector<long long>na;
struct fenwick{
long long fen[maxn];
void ins(long long i,long long w){
i++;
while(i<maxn){
fen[i]+=w;
i+=((-i)&i);
}
}
long long pors(long long i){
long long ret=0;
i++;
while(i>0){
ret+=fen[i];
i-=((-i)&i);
}
return ret;
}
}fensuma,fenssum,fensumkam;
void vorod(){
cin>>n>>d;
// if(n>3000){
// exit(0);
// }
for(long long i=1;i<=n;i++){
cin>>all[i];
}
cin>>q;
for(long long i=0;i<q;i++){
long long l,r;
cin>>l>>r;
qu[r].push_back(make_pair(l,i));
}
}
void ins(long long ind,long long w){
fensuma.ins(na.back()+1,w);
fensuma.ins(ind+1,-w);
fenssum.ins(na.back()+1,w*ind);
fenssum.ins(ind+1,-w*ind);
fensumkam.ins(0,w*(ind-na.back()));
fensumkam.ins(na.back()+1,-w*(ind-na.back()));
// for(long long i=ind;i>na.back();i--){
// suma[i]+=w;
// }
}
long long check(long long ind){
/*long long ted=suma[ind+1];
long long z=all[ind]-suma[ind+1]*d;
if(z>=all[ind-1]){
return -1;
}
ted=(all[ind-1]-z)/d;
if((all[ind-1]-z)%d!=0){
ted++;
}
return ted;*/
long long ted=0;
long long z=all[ind]-fensuma.pors(ind+1)*d;
if(z>=all[ind-1]){
return -1;
}
ted=(all[ind-1]-z)/d;
if((all[ind-1]-z)%d!=0){
ted++;
}
return ted;
}
long long pors(long long ind){
long long ret=0;
// for(long long i=ind+1;i<=n;i++){
// ret+=fensuma.pors(i);
// }
ret=fensumkam.pors(ind)+fenssum.pors(ind)-fensuma.pors(ind)*ind;
if(fensuma.pors(ind+1)*d>all[ind]){
return -1;
}
return ret;
}
void solve(){
na.push_back(1);
for(long long i=2;i<=n;i++){
if(all[i]<all[i-1]){
long long ted=(all[i-1]-all[i])/d;
if((all[i-1]-all[i])%d!=0){
ted++;
}
if(ted>0){
ins(i,ted);
}
while((long long)na.back()>1){
long long z=check(na.back());
if(z==-1){
break;
}
long long last=na.back();
na.pop_back();
ins(last,z);
}
}else{
na.push_back(i);
}
for(auto x:qu[i]){
res[x.second]=pors(x.first);
}
}
}
void khor(){
for(long long i=0;i<q;i++){
cout<<res[i]<<"\n";
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
// pre();
solve();
khor();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10584 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
6 ms |
14684 KB |
Output is correct |
4 |
Correct |
5 ms |
12888 KB |
Output is correct |
5 |
Correct |
5 ms |
12888 KB |
Output is correct |
6 |
Correct |
5 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
14892 KB |
Output is correct |
8 |
Correct |
5 ms |
12888 KB |
Output is correct |
9 |
Correct |
5 ms |
12812 KB |
Output is correct |
10 |
Correct |
5 ms |
14940 KB |
Output is correct |
11 |
Correct |
6 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
34944 KB |
Output is correct |
2 |
Correct |
147 ms |
30456 KB |
Output is correct |
3 |
Correct |
70 ms |
28496 KB |
Output is correct |
4 |
Correct |
233 ms |
32912 KB |
Output is correct |
5 |
Correct |
192 ms |
32888 KB |
Output is correct |
6 |
Correct |
192 ms |
31320 KB |
Output is correct |
7 |
Correct |
158 ms |
31224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
28264 KB |
Output is correct |
2 |
Correct |
205 ms |
40544 KB |
Output is correct |
3 |
Correct |
219 ms |
40532 KB |
Output is correct |
4 |
Correct |
251 ms |
40396 KB |
Output is correct |
5 |
Correct |
205 ms |
40528 KB |
Output is correct |
6 |
Correct |
221 ms |
34512 KB |
Output is correct |
7 |
Correct |
183 ms |
34252 KB |
Output is correct |
8 |
Correct |
234 ms |
37576 KB |
Output is correct |
9 |
Correct |
198 ms |
37580 KB |
Output is correct |
10 |
Correct |
180 ms |
35008 KB |
Output is correct |
11 |
Correct |
155 ms |
35012 KB |
Output is correct |
12 |
Correct |
198 ms |
39884 KB |
Output is correct |
13 |
Correct |
199 ms |
39972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
34336 KB |
Output is correct |
2 |
Correct |
238 ms |
33844 KB |
Output is correct |
3 |
Correct |
108 ms |
29968 KB |
Output is correct |
4 |
Correct |
186 ms |
36964 KB |
Output is correct |
5 |
Correct |
230 ms |
37204 KB |
Output is correct |
6 |
Correct |
247 ms |
38076 KB |
Output is correct |
7 |
Correct |
206 ms |
36352 KB |
Output is correct |
8 |
Correct |
232 ms |
38188 KB |
Output is correct |
9 |
Correct |
181 ms |
32992 KB |
Output is correct |
10 |
Correct |
167 ms |
34648 KB |
Output is correct |
11 |
Correct |
238 ms |
36932 KB |
Output is correct |
12 |
Correct |
190 ms |
38124 KB |
Output is correct |
13 |
Correct |
249 ms |
38052 KB |
Output is correct |
14 |
Correct |
228 ms |
33820 KB |
Output is correct |
15 |
Correct |
229 ms |
37720 KB |
Output is correct |
16 |
Correct |
225 ms |
33808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10584 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
6 ms |
14684 KB |
Output is correct |
4 |
Correct |
5 ms |
12888 KB |
Output is correct |
5 |
Correct |
5 ms |
12888 KB |
Output is correct |
6 |
Correct |
5 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
14892 KB |
Output is correct |
8 |
Correct |
5 ms |
12888 KB |
Output is correct |
9 |
Correct |
5 ms |
12812 KB |
Output is correct |
10 |
Correct |
5 ms |
14940 KB |
Output is correct |
11 |
Correct |
6 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
183 ms |
34944 KB |
Output is correct |
15 |
Correct |
147 ms |
30456 KB |
Output is correct |
16 |
Correct |
70 ms |
28496 KB |
Output is correct |
17 |
Correct |
233 ms |
32912 KB |
Output is correct |
18 |
Correct |
192 ms |
32888 KB |
Output is correct |
19 |
Correct |
192 ms |
31320 KB |
Output is correct |
20 |
Correct |
158 ms |
31224 KB |
Output is correct |
21 |
Correct |
77 ms |
28264 KB |
Output is correct |
22 |
Correct |
205 ms |
40544 KB |
Output is correct |
23 |
Correct |
219 ms |
40532 KB |
Output is correct |
24 |
Correct |
251 ms |
40396 KB |
Output is correct |
25 |
Correct |
205 ms |
40528 KB |
Output is correct |
26 |
Correct |
221 ms |
34512 KB |
Output is correct |
27 |
Correct |
183 ms |
34252 KB |
Output is correct |
28 |
Correct |
234 ms |
37576 KB |
Output is correct |
29 |
Correct |
198 ms |
37580 KB |
Output is correct |
30 |
Correct |
180 ms |
35008 KB |
Output is correct |
31 |
Correct |
155 ms |
35012 KB |
Output is correct |
32 |
Correct |
198 ms |
39884 KB |
Output is correct |
33 |
Correct |
199 ms |
39972 KB |
Output is correct |
34 |
Correct |
158 ms |
34336 KB |
Output is correct |
35 |
Correct |
238 ms |
33844 KB |
Output is correct |
36 |
Correct |
108 ms |
29968 KB |
Output is correct |
37 |
Correct |
186 ms |
36964 KB |
Output is correct |
38 |
Correct |
230 ms |
37204 KB |
Output is correct |
39 |
Correct |
247 ms |
38076 KB |
Output is correct |
40 |
Correct |
206 ms |
36352 KB |
Output is correct |
41 |
Correct |
232 ms |
38188 KB |
Output is correct |
42 |
Correct |
181 ms |
32992 KB |
Output is correct |
43 |
Correct |
167 ms |
34648 KB |
Output is correct |
44 |
Correct |
238 ms |
36932 KB |
Output is correct |
45 |
Correct |
190 ms |
38124 KB |
Output is correct |
46 |
Correct |
249 ms |
38052 KB |
Output is correct |
47 |
Correct |
228 ms |
33820 KB |
Output is correct |
48 |
Correct |
229 ms |
37720 KB |
Output is correct |
49 |
Correct |
225 ms |
33808 KB |
Output is correct |
50 |
Correct |
203 ms |
40528 KB |
Output is correct |
51 |
Correct |
204 ms |
34332 KB |
Output is correct |
52 |
Correct |
191 ms |
37580 KB |
Output is correct |
53 |
Correct |
180 ms |
33404 KB |
Output is correct |
54 |
Correct |
192 ms |
40072 KB |
Output is correct |
55 |
Correct |
233 ms |
40272 KB |
Output is correct |
56 |
Correct |
221 ms |
36008 KB |
Output is correct |
57 |
Correct |
234 ms |
32732 KB |
Output is correct |
58 |
Correct |
206 ms |
32956 KB |
Output is correct |
59 |
Correct |
232 ms |
38484 KB |
Output is correct |
60 |
Correct |
218 ms |
36068 KB |
Output is correct |
61 |
Correct |
154 ms |
35032 KB |
Output is correct |
62 |
Correct |
177 ms |
32968 KB |
Output is correct |
63 |
Correct |
206 ms |
39620 KB |
Output is correct |
64 |
Correct |
216 ms |
35924 KB |
Output is correct |