#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=5e5+5;
int mx[N*4],ans[N*4],add[N*4],a[N];
vector <pair <int,int> > qu[N];
vector <int> ab[N];
void build(int v,int tl,int tr){
if(tl==tr)mx[v]=a[tl];
else{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
mx[v]=max(mx[v*2],mx[v*2+1]);
}
}
void push(int v,int tl,int tr){
ans[v]=max(ans[v],add[v]+mx[v]);
if(tl!=tr){
add[v*2]=max(add[v*2],add[v]);
add[v*2+1]=max(add[v*2+1],add[v]);
}
add[v]=0;
}
void update(int v,int tl,int tr,int l,int r,int val){
if(add[v])push(v,tl,tr);
if(r<tl or tr<l)return;
if(l<=tl && tr<=r){
add[v]=val;
push(v,tl,tr);
return;
}
int tm=(tl+tr)/2;
update(v*2,tl,tm,l,r,val);
update(v*2+1,tm+1,tr,l,r,val);
ans[v]=max(ans[v*2],ans[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(add[v])push(v,tl,tr);
if(r<tl or tr<l)return 0;
if(l<=tl && tr<=r)return ans[v];
int tm=(tl+tr)/2;
return max(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
int q;
cin>>q;
for(int i=0;i<q;i++){
int l,r;
cin>>l>>r;
qu[l].pb({r,i});
}
stack <int> st;
for(int i=n;i>=1;i--){
while(!st.empty() && a[st.top()]<=a[i]){
ab[i].pb(st.top());
st.pop();
}
if(!st.empty())ab[i].pb(st.top());
st.push(i);
}
vector <int> res(q);
for(int l=n;l>=1;l--){
for(auto b : ab[l]){
update(1,1,n,b*2-l,n,a[l]+a[b]);
}
for(auto x : qu[l]){
res[x.ss]=get(1,1,n,l,x.ff);
}
}
for(auto x : res)cout<<x<<"\n";
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23996 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23996 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
226 ms |
51384 KB |
Output is correct |
12 |
Correct |
194 ms |
51280 KB |
Output is correct |
13 |
Correct |
273 ms |
51344 KB |
Output is correct |
14 |
Correct |
197 ms |
51328 KB |
Output is correct |
15 |
Correct |
207 ms |
51536 KB |
Output is correct |
16 |
Correct |
199 ms |
50680 KB |
Output is correct |
17 |
Correct |
200 ms |
50720 KB |
Output is correct |
18 |
Correct |
197 ms |
50512 KB |
Output is correct |
19 |
Correct |
185 ms |
51284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
46928 KB |
Output is correct |
2 |
Correct |
65 ms |
47320 KB |
Output is correct |
3 |
Correct |
81 ms |
45672 KB |
Output is correct |
4 |
Correct |
110 ms |
46928 KB |
Output is correct |
5 |
Correct |
111 ms |
47008 KB |
Output is correct |
6 |
Correct |
107 ms |
46276 KB |
Output is correct |
7 |
Correct |
105 ms |
46080 KB |
Output is correct |
8 |
Correct |
104 ms |
46160 KB |
Output is correct |
9 |
Correct |
117 ms |
46544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23996 KB |
Output is correct |
7 |
Correct |
9 ms |
23900 KB |
Output is correct |
8 |
Correct |
9 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
226 ms |
51384 KB |
Output is correct |
12 |
Correct |
194 ms |
51280 KB |
Output is correct |
13 |
Correct |
273 ms |
51344 KB |
Output is correct |
14 |
Correct |
197 ms |
51328 KB |
Output is correct |
15 |
Correct |
207 ms |
51536 KB |
Output is correct |
16 |
Correct |
199 ms |
50680 KB |
Output is correct |
17 |
Correct |
200 ms |
50720 KB |
Output is correct |
18 |
Correct |
197 ms |
50512 KB |
Output is correct |
19 |
Correct |
185 ms |
51284 KB |
Output is correct |
20 |
Correct |
115 ms |
46928 KB |
Output is correct |
21 |
Correct |
65 ms |
47320 KB |
Output is correct |
22 |
Correct |
81 ms |
45672 KB |
Output is correct |
23 |
Correct |
110 ms |
46928 KB |
Output is correct |
24 |
Correct |
111 ms |
47008 KB |
Output is correct |
25 |
Correct |
107 ms |
46276 KB |
Output is correct |
26 |
Correct |
105 ms |
46080 KB |
Output is correct |
27 |
Correct |
104 ms |
46160 KB |
Output is correct |
28 |
Correct |
117 ms |
46544 KB |
Output is correct |
29 |
Correct |
699 ms |
104020 KB |
Output is correct |
30 |
Correct |
601 ms |
105044 KB |
Output is correct |
31 |
Correct |
568 ms |
100952 KB |
Output is correct |
32 |
Correct |
751 ms |
104020 KB |
Output is correct |
33 |
Correct |
673 ms |
104020 KB |
Output is correct |
34 |
Correct |
758 ms |
101672 KB |
Output is correct |
35 |
Correct |
721 ms |
101460 KB |
Output is correct |
36 |
Correct |
720 ms |
101416 KB |
Output is correct |
37 |
Correct |
705 ms |
102952 KB |
Output is correct |
38 |
Correct |
488 ms |
106612 KB |
Output is correct |
39 |
Correct |
486 ms |
106692 KB |
Output is correct |
40 |
Correct |
526 ms |
103248 KB |
Output is correct |
41 |
Correct |
478 ms |
102736 KB |
Output is correct |
42 |
Correct |
475 ms |
102900 KB |
Output is correct |
43 |
Correct |
453 ms |
104420 KB |
Output is correct |
44 |
Correct |
541 ms |
106224 KB |
Output is correct |
45 |
Correct |
530 ms |
106320 KB |
Output is correct |
46 |
Correct |
523 ms |
103248 KB |
Output is correct |
47 |
Correct |
503 ms |
102740 KB |
Output is correct |
48 |
Correct |
542 ms |
102732 KB |
Output is correct |
49 |
Correct |
574 ms |
104708 KB |
Output is correct |
50 |
Correct |
582 ms |
105472 KB |
Output is correct |
51 |
Correct |
588 ms |
105600 KB |
Output is correct |
52 |
Correct |
576 ms |
103196 KB |
Output is correct |
53 |
Correct |
634 ms |
102692 KB |
Output is correct |
54 |
Correct |
573 ms |
102736 KB |
Output is correct |
55 |
Correct |
666 ms |
104528 KB |
Output is correct |