#include<bits/stdc++.h>
using namespace std;
const int N=500005;
struct node{
int res,f,mx;
node(){res=f=mx=0;}
node(int _res,int _f,int _mx){
res=_res;
f=_f;
mx=_mx;
}
};
struct SegTree{
node tree[1<<20];
int sz;
node Merge(node&a,node&b){
int res=max({a.res,b.res,a.f+b.mx}),f=max(a.f,b.f),mx=max(a.mx,b.mx);
return node(res,f,mx);
}
void build(int l,int r,int p,vector<int>&a){
if(l==r){
tree[p].f=-1e9;
tree[p].res=-1e9;
tree[p].mx=0;
if(l<a.size())tree[p].mx=a[l];
return;
}
int md=(l+r)>>1;
build(l,md,p*2+1,a);
build(md+1,r,p*2+2,a);
tree[p]=Merge(tree[p*2+1],tree[p*2+2]);
}
void init(int n,vector<int>&a){
sz=1;
while(sz<n)sz*=2;
build(0,sz-1,0,a);
}
void update(int l,int r,int p,int idx,int f){
if(l==r){
tree[p].f=max(tree[p].f,f);
tree[p].res=tree[p].f+tree[p].mx;
return;
}
int md=(l+r)>>1;
if(md>=idx)update(l,md,p*2+1,idx,f);
else update(md+1,r,p*2+2,idx,f);
tree[p]=Merge(tree[p*2+1],tree[p*2+2]);
}
void update(int idx,int f){
update(0,sz-1,0,idx,f);
}
node query(int l,int r,int p,int l_q,int r_q){
if(l>r_q||r<l_q)return node(0,0,0);
if(l>=l_q&&r<=r_q)return tree[p];
int md=(l+r)>>1;
node ch1=query(l,md,p*2+1,l_q,r_q);
node ch2=query(md+1,r,p*2+2,l_q,r_q);
return Merge(ch1,ch2);
}
int query(int l,int r){
return query(0,sz-1,0,l,r).res;
}
}tree;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector<pair<int,int>>seg;
vector<int>st;// decreasing stack
for(int i=0;i<n;i++){
while(!st.empty()&&a[i]>=a[st.back()]){
seg.push_back({st.back(),i});
st.pop_back();
}
if(!st.empty())seg.push_back({st.back(),i});
st.push_back(i);
}
sort(seg.begin(),seg.end());
tree.init(n,a);
int q;
cin>>q;
int L[q],R[q],sorted[q],ans[q];
for(int i=0;i<q;i++){
cin>>L[i]>>R[i];
L[i]--;R[i]--;
sorted[i]=i;
}
sort(sorted,sorted+q,[&](int i,int j){
return L[i]>L[j];
});
for(int i=0;i<q;i++){
while(!seg.empty()&&seg.back().first>=L[sorted[i]]){
auto[l,r]=seg.back();
if(2*r-l<n)tree.update(2*r-l,a[l]+a[r]);
seg.pop_back();
}
ans[sorted[i]]=tree.query(L[sorted[i]],R[sorted[i]]);
}
for(int i=0;i<q;i++)cout<<ans[i]<<"\n";
return 0;
}
Compilation message
jumps.cpp: In member function 'void SegTree::build(int, int, int, std::vector<int>&)':
jumps.cpp:25:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if(l<a.size())tree[p].mx=a[l];
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12628 KB |
Output is correct |
2 |
Correct |
6 ms |
12636 KB |
Output is correct |
3 |
Correct |
7 ms |
12628 KB |
Output is correct |
4 |
Correct |
8 ms |
12516 KB |
Output is correct |
5 |
Correct |
8 ms |
12568 KB |
Output is correct |
6 |
Correct |
7 ms |
12632 KB |
Output is correct |
7 |
Correct |
6 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12628 KB |
Output is correct |
10 |
Correct |
8 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12628 KB |
Output is correct |
2 |
Correct |
6 ms |
12636 KB |
Output is correct |
3 |
Correct |
7 ms |
12628 KB |
Output is correct |
4 |
Correct |
8 ms |
12516 KB |
Output is correct |
5 |
Correct |
8 ms |
12568 KB |
Output is correct |
6 |
Correct |
7 ms |
12632 KB |
Output is correct |
7 |
Correct |
6 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12628 KB |
Output is correct |
10 |
Correct |
8 ms |
12628 KB |
Output is correct |
11 |
Correct |
294 ms |
26860 KB |
Output is correct |
12 |
Correct |
298 ms |
26680 KB |
Output is correct |
13 |
Correct |
292 ms |
26716 KB |
Output is correct |
14 |
Correct |
285 ms |
26804 KB |
Output is correct |
15 |
Correct |
339 ms |
26812 KB |
Output is correct |
16 |
Correct |
286 ms |
26172 KB |
Output is correct |
17 |
Correct |
320 ms |
26140 KB |
Output is correct |
18 |
Correct |
316 ms |
26040 KB |
Output is correct |
19 |
Correct |
298 ms |
26684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
19268 KB |
Output is correct |
2 |
Correct |
59 ms |
17328 KB |
Output is correct |
3 |
Correct |
73 ms |
18500 KB |
Output is correct |
4 |
Correct |
114 ms |
19288 KB |
Output is correct |
5 |
Correct |
110 ms |
19280 KB |
Output is correct |
6 |
Correct |
106 ms |
18652 KB |
Output is correct |
7 |
Correct |
109 ms |
18636 KB |
Output is correct |
8 |
Correct |
122 ms |
18628 KB |
Output is correct |
9 |
Correct |
110 ms |
18896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12628 KB |
Output is correct |
2 |
Correct |
6 ms |
12636 KB |
Output is correct |
3 |
Correct |
7 ms |
12628 KB |
Output is correct |
4 |
Correct |
8 ms |
12516 KB |
Output is correct |
5 |
Correct |
8 ms |
12568 KB |
Output is correct |
6 |
Correct |
7 ms |
12632 KB |
Output is correct |
7 |
Correct |
6 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12628 KB |
Output is correct |
10 |
Correct |
8 ms |
12628 KB |
Output is correct |
11 |
Correct |
294 ms |
26860 KB |
Output is correct |
12 |
Correct |
298 ms |
26680 KB |
Output is correct |
13 |
Correct |
292 ms |
26716 KB |
Output is correct |
14 |
Correct |
285 ms |
26804 KB |
Output is correct |
15 |
Correct |
339 ms |
26812 KB |
Output is correct |
16 |
Correct |
286 ms |
26172 KB |
Output is correct |
17 |
Correct |
320 ms |
26140 KB |
Output is correct |
18 |
Correct |
316 ms |
26040 KB |
Output is correct |
19 |
Correct |
298 ms |
26684 KB |
Output is correct |
20 |
Correct |
110 ms |
19268 KB |
Output is correct |
21 |
Correct |
59 ms |
17328 KB |
Output is correct |
22 |
Correct |
73 ms |
18500 KB |
Output is correct |
23 |
Correct |
114 ms |
19288 KB |
Output is correct |
24 |
Correct |
110 ms |
19280 KB |
Output is correct |
25 |
Correct |
106 ms |
18652 KB |
Output is correct |
26 |
Correct |
109 ms |
18636 KB |
Output is correct |
27 |
Correct |
122 ms |
18628 KB |
Output is correct |
28 |
Correct |
110 ms |
18896 KB |
Output is correct |
29 |
Correct |
812 ms |
46336 KB |
Output is correct |
30 |
Correct |
607 ms |
42408 KB |
Output is correct |
31 |
Correct |
641 ms |
46300 KB |
Output is correct |
32 |
Correct |
715 ms |
46336 KB |
Output is correct |
33 |
Correct |
758 ms |
46476 KB |
Output is correct |
34 |
Correct |
722 ms |
44084 KB |
Output is correct |
35 |
Correct |
737 ms |
43684 KB |
Output is correct |
36 |
Correct |
751 ms |
43828 KB |
Output is correct |
37 |
Correct |
729 ms |
45196 KB |
Output is correct |
38 |
Correct |
588 ms |
46332 KB |
Output is correct |
39 |
Correct |
562 ms |
46332 KB |
Output is correct |
40 |
Correct |
595 ms |
42960 KB |
Output is correct |
41 |
Correct |
569 ms |
42488 KB |
Output is correct |
42 |
Correct |
642 ms |
42464 KB |
Output is correct |
43 |
Correct |
602 ms |
44248 KB |
Output is correct |
44 |
Correct |
593 ms |
46356 KB |
Output is correct |
45 |
Correct |
631 ms |
46368 KB |
Output is correct |
46 |
Correct |
603 ms |
43184 KB |
Output is correct |
47 |
Correct |
569 ms |
42800 KB |
Output is correct |
48 |
Correct |
560 ms |
42828 KB |
Output is correct |
49 |
Correct |
579 ms |
44772 KB |
Output is correct |
50 |
Correct |
604 ms |
46344 KB |
Output is correct |
51 |
Correct |
666 ms |
46560 KB |
Output is correct |
52 |
Correct |
606 ms |
43868 KB |
Output is correct |
53 |
Correct |
605 ms |
43612 KB |
Output is correct |
54 |
Correct |
593 ms |
43692 KB |
Output is correct |
55 |
Correct |
618 ms |
45212 KB |
Output is correct |