#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define ub(a,b) upper_bound(a.begin(),a.end(),b)-a.begin();
#define lb(a,b) lower_bound(a.begin(),a.end(),b)-a.begin();
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define sz(x) x.size()
using namespace std;
const int mxn=5e5+5;
int a[mxn]{0};
struct node{
int mx1,mx2,mx;
node operator+(node b){
return {max(mx1,b.mx1),max(mx2,b.mx2),max({mx,b.mx,mx2+b.mx1})};
}
}t[4*mxn];
void build(int i,int l,int r){
if(l==r)return void(t[i]={a[l],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].mx2=max(t[i].mx2,v);
t[i].mx=t[i].mx1+t[i].mx2;
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);
}sort(gd.rbegin(),gd.rend());
vector<pii>qr[n+1];int q;cin>>q;int ans[q+1];
for(int i=1;i<=q;i++){
int l,r;cin>>l>>r;
qr[l].pb({r,i});
}int idx=0;build(1,1,n);
for(int l=n;l>=1;l--){
while(idx<gd.size()&&gd[idx].f>=l){
if(2*gd[idx].s-gd[idx].f>n){idx++;continue;}
update(1,1,n,2*gd[idx].s-gd[idx].f,a[gd[idx].f]+a[gd[idx].s]);
idx++;
}
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:50:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
50 | if(!st.empty())gd.pb({st.top(),i});st.push(i);
| ^~
jumps.cpp:50:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
50 | if(!st.empty())gd.pb({st.top(),i});st.push(i);
| ^~
jumps.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(idx<gd.size()&&gd[idx].f>=l){
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
213 ms |
19284 KB |
Output is correct |
12 |
Correct |
222 ms |
19284 KB |
Output is correct |
13 |
Correct |
204 ms |
19364 KB |
Output is correct |
14 |
Correct |
199 ms |
19280 KB |
Output is correct |
15 |
Correct |
205 ms |
19352 KB |
Output is correct |
16 |
Correct |
217 ms |
18772 KB |
Output is correct |
17 |
Correct |
198 ms |
18772 KB |
Output is correct |
18 |
Correct |
186 ms |
18772 KB |
Output is correct |
19 |
Correct |
186 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
17080 KB |
Output is correct |
2 |
Correct |
50 ms |
15296 KB |
Output is correct |
3 |
Correct |
50 ms |
16152 KB |
Output is correct |
4 |
Correct |
88 ms |
17096 KB |
Output is correct |
5 |
Correct |
91 ms |
17092 KB |
Output is correct |
6 |
Correct |
88 ms |
16324 KB |
Output is correct |
7 |
Correct |
87 ms |
16316 KB |
Output is correct |
8 |
Correct |
100 ms |
16320 KB |
Output is correct |
9 |
Correct |
95 ms |
16584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
213 ms |
19284 KB |
Output is correct |
12 |
Correct |
222 ms |
19284 KB |
Output is correct |
13 |
Correct |
204 ms |
19364 KB |
Output is correct |
14 |
Correct |
199 ms |
19280 KB |
Output is correct |
15 |
Correct |
205 ms |
19352 KB |
Output is correct |
16 |
Correct |
217 ms |
18772 KB |
Output is correct |
17 |
Correct |
198 ms |
18772 KB |
Output is correct |
18 |
Correct |
186 ms |
18772 KB |
Output is correct |
19 |
Correct |
186 ms |
19284 KB |
Output is correct |
20 |
Correct |
94 ms |
17080 KB |
Output is correct |
21 |
Correct |
50 ms |
15296 KB |
Output is correct |
22 |
Correct |
50 ms |
16152 KB |
Output is correct |
23 |
Correct |
88 ms |
17096 KB |
Output is correct |
24 |
Correct |
91 ms |
17092 KB |
Output is correct |
25 |
Correct |
88 ms |
16324 KB |
Output is correct |
26 |
Correct |
87 ms |
16316 KB |
Output is correct |
27 |
Correct |
100 ms |
16320 KB |
Output is correct |
28 |
Correct |
95 ms |
16584 KB |
Output is correct |
29 |
Correct |
599 ms |
62132 KB |
Output is correct |
30 |
Correct |
496 ms |
58156 KB |
Output is correct |
31 |
Correct |
526 ms |
60168 KB |
Output is correct |
32 |
Correct |
592 ms |
62132 KB |
Output is correct |
33 |
Correct |
571 ms |
62132 KB |
Output is correct |
34 |
Correct |
571 ms |
59828 KB |
Output is correct |
35 |
Correct |
613 ms |
59572 KB |
Output is correct |
36 |
Correct |
637 ms |
59336 KB |
Output is correct |
37 |
Correct |
652 ms |
60912 KB |
Output is correct |
38 |
Correct |
503 ms |
67760 KB |
Output is correct |
39 |
Correct |
473 ms |
67764 KB |
Output is correct |
40 |
Correct |
458 ms |
64436 KB |
Output is correct |
41 |
Correct |
471 ms |
63924 KB |
Output is correct |
42 |
Correct |
459 ms |
63956 KB |
Output is correct |
43 |
Correct |
478 ms |
65524 KB |
Output is correct |
44 |
Correct |
517 ms |
66996 KB |
Output is correct |
45 |
Correct |
493 ms |
66996 KB |
Output is correct |
46 |
Correct |
475 ms |
63920 KB |
Output is correct |
47 |
Correct |
499 ms |
63640 KB |
Output is correct |
48 |
Correct |
523 ms |
63456 KB |
Output is correct |
49 |
Correct |
517 ms |
65712 KB |
Output is correct |
50 |
Correct |
559 ms |
65236 KB |
Output is correct |
51 |
Correct |
537 ms |
65188 KB |
Output is correct |
52 |
Correct |
502 ms |
62640 KB |
Output is correct |
53 |
Correct |
538 ms |
62612 KB |
Output is correct |
54 |
Correct |
495 ms |
62388 KB |
Output is correct |
55 |
Correct |
542 ms |
64180 KB |
Output is correct |