#include <bits/stdc++.h>
using namespace std;
const int nmax=500005;
vector< pair<int,int> > l[nmax],qr[nmax];
int v[nmax],st[nmax],an[nmax];
int n,i,j,u,q,L,R;
struct node
{
int L,R,LR;
}arb[4*nmax],ans;
void add(int x,int y)
{
if(2*y-x<=n)
l[x].push_back({2*y-x,v[x]+v[y]});
}
void mrg(node &A,node B,node C)
{
A.L=max(B.L,C.L);
A.R=max(B.R,C.R);
A.LR=max(B.LR,C.LR);
A.LR=max(A.LR,B.L+C.R);
}
void update(int nod,int l,int r,int poz,int val,int tip)
{
if(l==r)
{
if(tip) arb[nod].R=max(arb[nod].R,val);
else arb[nod].L=max(arb[nod].L,val);
arb[nod].LR=arb[nod].L+arb[nod].R;
return;
}
int m=(l+r)/2;
if(poz<=m) update(2*nod,l,m,poz,val,tip);
else update(2*nod+1,m+1,r,poz,val,tip);
mrg(arb[nod],arb[2*nod],arb[2*nod+1]);
}
void query(int nod,int l,int r,int st,int dr)
{
if(st<=l&&r<=dr)
{
mrg(ans,ans,arb[nod]);
return;
}
int m=(l+r)/2;
if(st<=m) query(2*nod,l,m,st,dr);
if(m<dr) query(2*nod+1,m+1,r,st,dr);
}
int Q(int S,int D)
{
ans={-(1<<29),-(1<<29),-(1<<29)};
query(1,1,n,S,D);
return ans.LR;
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n;
for(i=1;i<=n;i++)
{
cin>>v[i];
while(u>0&&v[i]>v[st[u]])
{
add(st[u],i);
u--;
}
if(u) add(st[u],i);
st[++u]=i;
}
cin>>q;
for(i=1;i<=q;i++)
{
cin>>L>>R;
qr[L].push_back({R,i});
}
for(i=1;i<=4*n;i++)
{
arb[i].L=arb[i].R=arb[i].LR=-(1<<29);
}
for(i=n;i>=1;i--)
{
update(1,1,n,i,v[i],1);
for(auto it: l[i])
update(1,1,n,it.first,it.second,0);
for(auto it: qr[i])
an[it.second]=Q(i,it.first);
}
for(i=1;i<=q;i++)
cout<<an[i]<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
24060 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
24060 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
311 ms |
42364 KB |
Output is correct |
12 |
Correct |
339 ms |
42496 KB |
Output is correct |
13 |
Correct |
339 ms |
42360 KB |
Output is correct |
14 |
Correct |
324 ms |
42488 KB |
Output is correct |
15 |
Correct |
322 ms |
42380 KB |
Output is correct |
16 |
Correct |
331 ms |
41724 KB |
Output is correct |
17 |
Correct |
317 ms |
41692 KB |
Output is correct |
18 |
Correct |
326 ms |
41720 KB |
Output is correct |
19 |
Correct |
321 ms |
42212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
43256 KB |
Output is correct |
2 |
Correct |
151 ms |
42076 KB |
Output is correct |
3 |
Correct |
151 ms |
42844 KB |
Output is correct |
4 |
Correct |
212 ms |
43384 KB |
Output is correct |
5 |
Correct |
211 ms |
43256 KB |
Output is correct |
6 |
Correct |
204 ms |
42540 KB |
Output is correct |
7 |
Correct |
204 ms |
42616 KB |
Output is correct |
8 |
Correct |
204 ms |
42656 KB |
Output is correct |
9 |
Correct |
212 ms |
42964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
24056 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
24 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
24060 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
311 ms |
42364 KB |
Output is correct |
12 |
Correct |
339 ms |
42496 KB |
Output is correct |
13 |
Correct |
339 ms |
42360 KB |
Output is correct |
14 |
Correct |
324 ms |
42488 KB |
Output is correct |
15 |
Correct |
322 ms |
42380 KB |
Output is correct |
16 |
Correct |
331 ms |
41724 KB |
Output is correct |
17 |
Correct |
317 ms |
41692 KB |
Output is correct |
18 |
Correct |
326 ms |
41720 KB |
Output is correct |
19 |
Correct |
321 ms |
42212 KB |
Output is correct |
20 |
Correct |
211 ms |
43256 KB |
Output is correct |
21 |
Correct |
151 ms |
42076 KB |
Output is correct |
22 |
Correct |
151 ms |
42844 KB |
Output is correct |
23 |
Correct |
212 ms |
43384 KB |
Output is correct |
24 |
Correct |
211 ms |
43256 KB |
Output is correct |
25 |
Correct |
204 ms |
42540 KB |
Output is correct |
26 |
Correct |
204 ms |
42616 KB |
Output is correct |
27 |
Correct |
204 ms |
42656 KB |
Output is correct |
28 |
Correct |
212 ms |
42964 KB |
Output is correct |
29 |
Correct |
1152 ms |
96064 KB |
Output is correct |
30 |
Correct |
1020 ms |
92860 KB |
Output is correct |
31 |
Correct |
997 ms |
94584 KB |
Output is correct |
32 |
Correct |
1214 ms |
96120 KB |
Output is correct |
33 |
Correct |
1171 ms |
96036 KB |
Output is correct |
34 |
Correct |
1148 ms |
93632 KB |
Output is correct |
35 |
Correct |
1135 ms |
93268 KB |
Output is correct |
36 |
Correct |
1138 ms |
93188 KB |
Output is correct |
37 |
Correct |
1127 ms |
94668 KB |
Output is correct |
38 |
Correct |
844 ms |
101608 KB |
Output is correct |
39 |
Correct |
846 ms |
101604 KB |
Output is correct |
40 |
Correct |
819 ms |
98228 KB |
Output is correct |
41 |
Correct |
835 ms |
97856 KB |
Output is correct |
42 |
Correct |
814 ms |
97780 KB |
Output is correct |
43 |
Correct |
826 ms |
99408 KB |
Output is correct |
44 |
Correct |
895 ms |
100984 KB |
Output is correct |
45 |
Correct |
907 ms |
100948 KB |
Output is correct |
46 |
Correct |
878 ms |
97940 KB |
Output is correct |
47 |
Correct |
860 ms |
97496 KB |
Output is correct |
48 |
Correct |
868 ms |
97340 KB |
Output is correct |
49 |
Correct |
881 ms |
99528 KB |
Output is correct |
50 |
Correct |
1000 ms |
99132 KB |
Output is correct |
51 |
Correct |
962 ms |
99044 KB |
Output is correct |
52 |
Correct |
951 ms |
96704 KB |
Output is correct |
53 |
Correct |
966 ms |
96244 KB |
Output is correct |
54 |
Correct |
985 ms |
96260 KB |
Output is correct |
55 |
Correct |
959 ms |
97924 KB |
Output is correct |