#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int mxn=5e5+5;
int a[mxn];
struct node{
int mx,mxf,mxa;
node operator+(node b){
node rs;
rs.mx=max({mx,b.mx,mxf+b.mxa});
rs.mxf=max(mxf,b.mxf);
rs.mxa=max(mxa,b.mxa);
return rs;
}
}t[4*mxn];
void build(int i,int l,int r){
if(l==r)return void(t[i]={0,0,a[l]});
int m=(l+r)>>1;build(2*i,l,m);build(2*i+1,m+1,r);
t[i]=t[2*i]+t[2*i+1];
}
void update(int i,int l,int r,int idx,int v){
if(r<idx||l>idx)return;
if(l==r){
t[i].mxf=max(t[i].mxf,v);
t[i].mx=t[i].mxf+t[i].mxa;
return;
}int m=(l+r)>>1;update(2*i,l,m,idx,v);update(2*i+1,m+1,r,idx,v);
t[i]=t[2*i]+t[2*i+1];
}
node query(int i,int l,int r,int tl,int tr){
if(r<tl||l>tr)return {0,0,0};
if(r<=tr&&l>=tl)return t[i];
int m=(l+r)>>1;
return query(2*i,l,m,tl,tr)+query(2*i+1,m+1,r,tl,tr);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
stack<int>st;vector<pii>gd;
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]<=a[i])gd.pb({st.top(),i}),st.pop();
if(!st.empty())gd.pb({st.top(),i});st.push(i);
}int id=0;build(1,1,n);
int q;cin>>q;int ans[q+1];
vector<pii>qr[n+1];
for(int i=1;i<=q;i++){
int l,r;cin>>l>>r;
qr[l].pb({r,i});
}sort(gd.rbegin(),gd.rend());
for(int l=n;l>=1;l--){
while(id<gd.size()&&gd[id].f>=l){id++;
int idx=gd[id-1].s*2-gd[id-1].f;
if(idx>n){continue;}
update(1,1,n,idx,a[gd[id-1].f]+a[gd[id-1].s]);
}
for(auto it : qr[l]){
ans[it.s]=query(1,1,n,l,it.f).mx;
}
}for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:48:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
48 | if(!st.empty())gd.pb({st.top(),i});st.push(i);
| ^~
jumps.cpp:48:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
48 | if(!st.empty())gd.pb({st.top(),i});st.push(i);
| ^~
jumps.cpp:57:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(id<gd.size()&&gd[id].f>=l){id++;
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
221 ms |
25300 KB |
Output is correct |
12 |
Correct |
224 ms |
25112 KB |
Output is correct |
13 |
Correct |
219 ms |
25300 KB |
Output is correct |
14 |
Correct |
216 ms |
25304 KB |
Output is correct |
15 |
Correct |
214 ms |
25244 KB |
Output is correct |
16 |
Correct |
226 ms |
24792 KB |
Output is correct |
17 |
Correct |
237 ms |
24792 KB |
Output is correct |
18 |
Correct |
226 ms |
24536 KB |
Output is correct |
19 |
Correct |
221 ms |
25048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
21196 KB |
Output is correct |
2 |
Correct |
54 ms |
16584 KB |
Output is correct |
3 |
Correct |
50 ms |
17392 KB |
Output is correct |
4 |
Correct |
106 ms |
21180 KB |
Output is correct |
5 |
Correct |
117 ms |
19908 KB |
Output is correct |
6 |
Correct |
103 ms |
20452 KB |
Output is correct |
7 |
Correct |
104 ms |
21696 KB |
Output is correct |
8 |
Correct |
99 ms |
19912 KB |
Output is correct |
9 |
Correct |
101 ms |
20684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
221 ms |
25300 KB |
Output is correct |
12 |
Correct |
224 ms |
25112 KB |
Output is correct |
13 |
Correct |
219 ms |
25300 KB |
Output is correct |
14 |
Correct |
216 ms |
25304 KB |
Output is correct |
15 |
Correct |
214 ms |
25244 KB |
Output is correct |
16 |
Correct |
226 ms |
24792 KB |
Output is correct |
17 |
Correct |
237 ms |
24792 KB |
Output is correct |
18 |
Correct |
226 ms |
24536 KB |
Output is correct |
19 |
Correct |
221 ms |
25048 KB |
Output is correct |
20 |
Correct |
104 ms |
21196 KB |
Output is correct |
21 |
Correct |
54 ms |
16584 KB |
Output is correct |
22 |
Correct |
50 ms |
17392 KB |
Output is correct |
23 |
Correct |
106 ms |
21180 KB |
Output is correct |
24 |
Correct |
117 ms |
19908 KB |
Output is correct |
25 |
Correct |
103 ms |
20452 KB |
Output is correct |
26 |
Correct |
104 ms |
21696 KB |
Output is correct |
27 |
Correct |
99 ms |
19912 KB |
Output is correct |
28 |
Correct |
101 ms |
20684 KB |
Output is correct |
29 |
Correct |
719 ms |
71132 KB |
Output is correct |
30 |
Correct |
558 ms |
66352 KB |
Output is correct |
31 |
Correct |
554 ms |
68412 KB |
Output is correct |
32 |
Correct |
730 ms |
74672 KB |
Output is correct |
33 |
Correct |
682 ms |
74156 KB |
Output is correct |
34 |
Correct |
719 ms |
71852 KB |
Output is correct |
35 |
Correct |
672 ms |
71448 KB |
Output is correct |
36 |
Correct |
670 ms |
71344 KB |
Output is correct |
37 |
Correct |
671 ms |
73136 KB |
Output is correct |
38 |
Correct |
561 ms |
76716 KB |
Output is correct |
39 |
Correct |
556 ms |
76464 KB |
Output is correct |
40 |
Correct |
539 ms |
73392 KB |
Output is correct |
41 |
Correct |
534 ms |
72720 KB |
Output is correct |
42 |
Correct |
518 ms |
72812 KB |
Output is correct |
43 |
Correct |
543 ms |
74512 KB |
Output is correct |
44 |
Correct |
590 ms |
76780 KB |
Output is correct |
45 |
Correct |
594 ms |
77492 KB |
Output is correct |
46 |
Correct |
570 ms |
73648 KB |
Output is correct |
47 |
Correct |
586 ms |
73268 KB |
Output is correct |
48 |
Correct |
591 ms |
73816 KB |
Output is correct |
49 |
Correct |
574 ms |
75436 KB |
Output is correct |
50 |
Correct |
637 ms |
77080 KB |
Output is correct |
51 |
Correct |
626 ms |
76976 KB |
Output is correct |
52 |
Correct |
625 ms |
74672 KB |
Output is correct |
53 |
Correct |
599 ms |
74852 KB |
Output is correct |
54 |
Correct |
627 ms |
74156 KB |
Output is correct |
55 |
Correct |
620 ms |
76172 KB |
Output is correct |