#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
int n,all[maxn],q,res[maxn],kaf=(1<<19);
vector<pair<int,int>>allq[maxn],updy[maxn];
struct segment{
struct node{
int mx,lazy,res;
node(){
mx=lazy=res=0;
}
};
node seg[(1<<20)];
void calc(int i){
if(i>=kaf){
seg[i].res=max(seg[i].res,seg[i].mx+seg[i].lazy);
seg[i].lazy=0;
return ;
}
seg[i].res=max(seg[i].res,max(seg[(i<<1)].res,seg[(i<<1)^1].res));
seg[i].mx=max(seg[(i<<1)].mx,seg[(i<<1)^1].mx);
}
void shift(int i){
if(i>=kaf){
return calc(i);
}
seg[(i<<1)].lazy=max(seg[i].lazy,seg[(i<<1)].lazy);
seg[(i<<1)^1].lazy=max(seg[i].lazy,seg[(i<<1)^1].lazy);
seg[i].lazy=0;
seg[(i<<1)].res=max(seg[(i<<1)].res,seg[(i<<1)].lazy+seg[(i<<1)].mx);
seg[(i<<1)^1].res=max(seg[(i<<1)^1].res,seg[(i<<1)^1].lazy+seg[(i<<1)^1].mx);
}
void ins(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
// cout<<"ins: "<<l<<" "<<r<<" "<<w<<endl;
calc(i);
seg[i].mx=max(w,seg[i].mx);
seg[i].lazy=0;
seg[i].res=max(seg[i].res,w);
return ;
}
shift(i);
int m=(l+r)>>1;
ins((i<<1),l,m,tl,tr,w);
ins((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
}
void upd(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
// cout<<"upd: "<<l<<" "<<r<<" "<<w<<endl;
seg[i].lazy=max(seg[i].lazy,w);
seg[i].res=max(seg[i].res,seg[i].mx+seg[i].lazy);
return ;
}
shift(i);
int m=(l+r)>>1;
upd((i<<1),l,m,tl,tr,w);
upd((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
}
int pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return 0;
}
if(l>=tl&&r<=tr){
return seg[i].res;
}
shift(i);
calc(i);
int m=(l+r)>>1;
return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}seg;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>all[i];
}
cin>>q;
for(int i=1;i<=q;i++){
int l,r;
cin>>l>>r;
allq[r].push_back(make_pair(l,i));
}
vector<int>v;
for(int i=1;i<=n;i++){
for(auto x:updy[i]){
seg.ins(1,0,kaf-1,x.first,x.first,x.second);
}
seg.upd(1,0,kaf-1,0,i,all[i]);
for(auto x:allq[i]){
res[x.second]=seg.pors(1,0,kaf-1,x.first,i);
}
while((int)v.size()>0&&all[v.back()]<=all[i]){
updy[min(n+1,i+(i-v.back()))].push_back(make_pair(v.back(),all[i]+all[v.back()]));
v.pop_back();
}
if((int)v.size()>0){
updy[min(n+1,i+(i-v.back()))].push_back(make_pair(v.back(),all[i]+all[v.back()]));
}
v.push_back(i);
}
for(int i=1;i<=q;i++){
cout<<res[i]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
39616 KB |
Output is correct |
2 |
Correct |
10 ms |
39516 KB |
Output is correct |
3 |
Correct |
9 ms |
39608 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39620 KB |
Output is correct |
6 |
Correct |
9 ms |
39512 KB |
Output is correct |
7 |
Correct |
9 ms |
39512 KB |
Output is correct |
8 |
Correct |
9 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
39616 KB |
Output is correct |
10 |
Correct |
9 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
39616 KB |
Output is correct |
2 |
Correct |
10 ms |
39516 KB |
Output is correct |
3 |
Correct |
9 ms |
39608 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39620 KB |
Output is correct |
6 |
Correct |
9 ms |
39512 KB |
Output is correct |
7 |
Correct |
9 ms |
39512 KB |
Output is correct |
8 |
Correct |
9 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
39616 KB |
Output is correct |
10 |
Correct |
9 ms |
39516 KB |
Output is correct |
11 |
Correct |
282 ms |
56144 KB |
Output is correct |
12 |
Correct |
280 ms |
55892 KB |
Output is correct |
13 |
Correct |
275 ms |
56148 KB |
Output is correct |
14 |
Correct |
307 ms |
56052 KB |
Output is correct |
15 |
Correct |
280 ms |
56128 KB |
Output is correct |
16 |
Correct |
298 ms |
55576 KB |
Output is correct |
17 |
Correct |
281 ms |
55352 KB |
Output is correct |
18 |
Correct |
312 ms |
55308 KB |
Output is correct |
19 |
Correct |
286 ms |
56112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
48188 KB |
Output is correct |
2 |
Correct |
136 ms |
47600 KB |
Output is correct |
3 |
Correct |
141 ms |
48260 KB |
Output is correct |
4 |
Correct |
224 ms |
48176 KB |
Output is correct |
5 |
Correct |
207 ms |
48352 KB |
Output is correct |
6 |
Correct |
222 ms |
47696 KB |
Output is correct |
7 |
Correct |
203 ms |
47440 KB |
Output is correct |
8 |
Correct |
205 ms |
47696 KB |
Output is correct |
9 |
Correct |
223 ms |
47896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
39616 KB |
Output is correct |
2 |
Correct |
10 ms |
39516 KB |
Output is correct |
3 |
Correct |
9 ms |
39608 KB |
Output is correct |
4 |
Correct |
10 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39620 KB |
Output is correct |
6 |
Correct |
9 ms |
39512 KB |
Output is correct |
7 |
Correct |
9 ms |
39512 KB |
Output is correct |
8 |
Correct |
9 ms |
39516 KB |
Output is correct |
9 |
Correct |
9 ms |
39616 KB |
Output is correct |
10 |
Correct |
9 ms |
39516 KB |
Output is correct |
11 |
Correct |
282 ms |
56144 KB |
Output is correct |
12 |
Correct |
280 ms |
55892 KB |
Output is correct |
13 |
Correct |
275 ms |
56148 KB |
Output is correct |
14 |
Correct |
307 ms |
56052 KB |
Output is correct |
15 |
Correct |
280 ms |
56128 KB |
Output is correct |
16 |
Correct |
298 ms |
55576 KB |
Output is correct |
17 |
Correct |
281 ms |
55352 KB |
Output is correct |
18 |
Correct |
312 ms |
55308 KB |
Output is correct |
19 |
Correct |
286 ms |
56112 KB |
Output is correct |
20 |
Correct |
207 ms |
48188 KB |
Output is correct |
21 |
Correct |
136 ms |
47600 KB |
Output is correct |
22 |
Correct |
141 ms |
48260 KB |
Output is correct |
23 |
Correct |
224 ms |
48176 KB |
Output is correct |
24 |
Correct |
207 ms |
48352 KB |
Output is correct |
25 |
Correct |
222 ms |
47696 KB |
Output is correct |
26 |
Correct |
203 ms |
47440 KB |
Output is correct |
27 |
Correct |
205 ms |
47696 KB |
Output is correct |
28 |
Correct |
223 ms |
47896 KB |
Output is correct |
29 |
Correct |
1045 ms |
83540 KB |
Output is correct |
30 |
Correct |
797 ms |
81480 KB |
Output is correct |
31 |
Correct |
849 ms |
83540 KB |
Output is correct |
32 |
Correct |
1089 ms |
83628 KB |
Output is correct |
33 |
Correct |
1025 ms |
83624 KB |
Output is correct |
34 |
Correct |
987 ms |
81264 KB |
Output is correct |
35 |
Correct |
1036 ms |
80912 KB |
Output is correct |
36 |
Correct |
1000 ms |
81200 KB |
Output is correct |
37 |
Correct |
1016 ms |
82544 KB |
Output is correct |
38 |
Correct |
818 ms |
89168 KB |
Output is correct |
39 |
Correct |
802 ms |
89140 KB |
Output is correct |
40 |
Correct |
789 ms |
85932 KB |
Output is correct |
41 |
Correct |
802 ms |
85828 KB |
Output is correct |
42 |
Correct |
817 ms |
85504 KB |
Output is correct |
43 |
Correct |
814 ms |
87004 KB |
Output is correct |
44 |
Correct |
842 ms |
88588 KB |
Output is correct |
45 |
Correct |
872 ms |
88652 KB |
Output is correct |
46 |
Correct |
827 ms |
85608 KB |
Output is correct |
47 |
Correct |
833 ms |
84900 KB |
Output is correct |
48 |
Correct |
822 ms |
85044 KB |
Output is correct |
49 |
Correct |
829 ms |
87360 KB |
Output is correct |
50 |
Correct |
894 ms |
86548 KB |
Output is correct |
51 |
Correct |
938 ms |
86472 KB |
Output is correct |
52 |
Correct |
916 ms |
84304 KB |
Output is correct |
53 |
Correct |
879 ms |
83732 KB |
Output is correct |
54 |
Correct |
885 ms |
83648 KB |
Output is correct |
55 |
Correct |
896 ms |
85404 KB |
Output is correct |