long long lo(long long a,long long b){return a<b?a:b;}
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<vector>
#include<utility>
#define N 300005
int n,q;
long long d,c[N],pc[N],zz[N];
long long tt[N<<2],lz[N<<2];
void push(int v,int l,int r)
{
if(~lz[v])
{
tt[v]=1ll*(r-l+1)*lz[v];
if(l-r)lz[2*v+1]=lz[2*v+2]=lz[v];
lz[v]=-1;
}
}
void upd(int v,int l,int r,int x,int y,long long k)
{
push(v,l,r);
if(r<x||y<l)return;
if(x<=l&&r<=y) { lz[v]=k;push(v,l,r);return; }
upd(2*v+1,l,(l+r)/2,x,y,k);
upd(2*v+2,(l+r)/2+1,r,x,y,k);
tt[v]=tt[2*v+1]+tt[2*v+2];
}
long long qry(int v,int l,int r,int x,int y)
{
push(v,l,r);
if(r<x||y<l)return 0;
if(x<=l&&r<=y)return tt[v];
return qry(2*v+1,l,l+r>>1,x,y)+qry(2*v+2,(l+r)/2+1,r,x,y);
}
std::vector<std::pair<int, int> > qryo[N];
int stk[N],top,prev_smol[N];
int main()
{
scanf("%d%lld",&n,&d);
if(d-1)__builtin_trap();
memset(lz,-1,sizeof lz);
for(int i=1;i<=n;++i)
{
scanf("%lld",c+i),pc[i]=pc[i-1]+c[i];
}
for(int i=n;i>=1;stk[top++]=i--)
while(top&&c[stk[top-1]]>=c[i])prev_smol[stk[--top]]=i;
scanf("%d",&q);
for(int l,r,i=0;i<q;++i)
scanf("%d%d",&l,&r),
qryo[r].push_back(std::make_pair(l,i));
for(int i=1;i<=n;++i)
{
upd(0,1,n,prev_smol[i]+1,i,c[i]);
for(auto[l,I]:qryo[i])
{
zz[I]=pc[i]-pc[l-1]-qry(0,1,n,l,i);
}
}
for(int i=0;i<q;++i)printf("%lld\n",zz[i]);
return 0;
}
Compilation message
Main.cpp: In function 'long long int qry(int, int, int, int, int)':
Main.cpp:39:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | return qry(2*v+1,l,l+r>>1,x,y)+qry(2*v+2,(l+r)/2+1,r,x,y);
| ~^~
Main.cpp: In function 'int main()':
Main.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%lld",&n,&d);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%lld",c+i),pc[i]=pc[i-1]+c[i];
| ~~~~~^~~~~~~~~~~~
Main.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
Main.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d",&l,&r),
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
9 ms |
14684 KB |
Execution killed with signal 4 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
45500 KB |
Output is correct |
2 |
Correct |
370 ms |
44728 KB |
Output is correct |
3 |
Runtime error |
9 ms |
14680 KB |
Execution killed with signal 4 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
30916 KB |
Output is correct |
2 |
Correct |
379 ms |
52304 KB |
Output is correct |
3 |
Correct |
444 ms |
52352 KB |
Output is correct |
4 |
Correct |
383 ms |
52052 KB |
Output is correct |
5 |
Correct |
388 ms |
52364 KB |
Output is correct |
6 |
Correct |
355 ms |
45616 KB |
Output is correct |
7 |
Correct |
349 ms |
45700 KB |
Output is correct |
8 |
Correct |
366 ms |
48996 KB |
Output is correct |
9 |
Correct |
366 ms |
48888 KB |
Output is correct |
10 |
Correct |
367 ms |
47676 KB |
Output is correct |
11 |
Correct |
348 ms |
47444 KB |
Output is correct |
12 |
Correct |
375 ms |
50508 KB |
Output is correct |
13 |
Correct |
377 ms |
50688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
39760 KB |
Output is correct |
2 |
Correct |
371 ms |
45600 KB |
Output is correct |
3 |
Correct |
203 ms |
32784 KB |
Output is correct |
4 |
Correct |
352 ms |
48736 KB |
Output is correct |
5 |
Correct |
357 ms |
50472 KB |
Output is correct |
6 |
Correct |
441 ms |
51972 KB |
Output is correct |
7 |
Correct |
344 ms |
45164 KB |
Output is correct |
8 |
Correct |
384 ms |
51916 KB |
Output is correct |
9 |
Runtime error |
10 ms |
14684 KB |
Execution killed with signal 4 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
9 ms |
14684 KB |
Execution killed with signal 4 |
3 |
Halted |
0 ms |
0 KB |
- |