#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define sz(a) (int)a.size()
using ar2 = array<int,2>;
const int N = (int)1e6+10;
const int INF = (int)1e18;
int n, q, a[N], ans[N];
vector<ar2> v[N], Q[N];
stack<int> S;
struct Data{
int mxSm=-INF, mxA=-INF, ans=-INF;
} seg[2*N];
Data comb(Data a, Data b){
Data c;
c.mxSm = max(a.mxSm, b.mxSm);
c.mxA = max(a.mxA, b.mxA);
c.ans = max({a.ans,b.ans,a.mxSm+b.mxA});
return c;
}
void upd(int x, int v, int p=0, int l=1, int r=n){
if(l==r){
seg[p].mxSm = max(seg[p].mxSm, v);
seg[p].mxA = max(seg[p].mxA,a[x]);
seg[p].ans = seg[p].mxSm+seg[p].mxA;
return;
}
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
if(x<=mid) upd(x,v,p+1,l,mid);
else upd(x,v,rp,mid+1,r);
seg[p] = comb(seg[p+1],seg[rp]);
}
Data query(int i, int j, int p=0, int l=1, int r=n){
if(i>r or j<l or i>j) return {-INF,-INF,-INF};
if(i<=l and r<=j) return seg[p];
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
return comb(query(i,j,p+1,l,mid),query(i,j,rp,mid+1,r));
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = n,j; i >= 1; i--){
while(sz(S) and a[S.top()]<a[i])
j = S.top(), v[i].pb({j,a[i]+a[j]}),S.pop();
if(sz(S)) j = S.top(), v[i].pb({j,a[i]+a[j]});
S.push(i);
}
cin >> q;
for(int l,r,i=1; i <= q; i++)
cin >> l >> r, Q[l].pb({r,i});
for(int i = n; i >= 1; i--){
for(auto [j,sum] : v[i]) if(2*j-i<=n) upd(2*j-i,sum);
for(auto [j,ind] : Q[i]) ans[ind] = query(i,j).ans;
}
for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47404 KB |
Output is correct |
2 |
Correct |
34 ms |
47448 KB |
Output is correct |
3 |
Correct |
44 ms |
47440 KB |
Output is correct |
4 |
Correct |
35 ms |
47440 KB |
Output is correct |
5 |
Correct |
42 ms |
47340 KB |
Output is correct |
6 |
Correct |
35 ms |
47316 KB |
Output is correct |
7 |
Correct |
44 ms |
47440 KB |
Output is correct |
8 |
Correct |
37 ms |
47432 KB |
Output is correct |
9 |
Correct |
40 ms |
47440 KB |
Output is correct |
10 |
Correct |
40 ms |
47472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47404 KB |
Output is correct |
2 |
Correct |
34 ms |
47448 KB |
Output is correct |
3 |
Correct |
44 ms |
47440 KB |
Output is correct |
4 |
Correct |
35 ms |
47440 KB |
Output is correct |
5 |
Correct |
42 ms |
47340 KB |
Output is correct |
6 |
Correct |
35 ms |
47316 KB |
Output is correct |
7 |
Correct |
44 ms |
47440 KB |
Output is correct |
8 |
Correct |
37 ms |
47432 KB |
Output is correct |
9 |
Correct |
40 ms |
47440 KB |
Output is correct |
10 |
Correct |
40 ms |
47472 KB |
Output is correct |
11 |
Correct |
306 ms |
74824 KB |
Output is correct |
12 |
Correct |
351 ms |
74728 KB |
Output is correct |
13 |
Correct |
350 ms |
74820 KB |
Output is correct |
14 |
Correct |
288 ms |
74828 KB |
Output is correct |
15 |
Correct |
297 ms |
74968 KB |
Output is correct |
16 |
Correct |
301 ms |
74320 KB |
Output is correct |
17 |
Correct |
368 ms |
74280 KB |
Output is correct |
18 |
Correct |
355 ms |
74060 KB |
Output is correct |
19 |
Correct |
407 ms |
74600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
70360 KB |
Output is correct |
2 |
Correct |
141 ms |
67888 KB |
Output is correct |
3 |
Correct |
138 ms |
66236 KB |
Output is correct |
4 |
Correct |
223 ms |
70360 KB |
Output is correct |
5 |
Correct |
191 ms |
70352 KB |
Output is correct |
6 |
Correct |
205 ms |
69704 KB |
Output is correct |
7 |
Correct |
203 ms |
69416 KB |
Output is correct |
8 |
Correct |
215 ms |
69464 KB |
Output is correct |
9 |
Correct |
199 ms |
69960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47404 KB |
Output is correct |
2 |
Correct |
34 ms |
47448 KB |
Output is correct |
3 |
Correct |
44 ms |
47440 KB |
Output is correct |
4 |
Correct |
35 ms |
47440 KB |
Output is correct |
5 |
Correct |
42 ms |
47340 KB |
Output is correct |
6 |
Correct |
35 ms |
47316 KB |
Output is correct |
7 |
Correct |
44 ms |
47440 KB |
Output is correct |
8 |
Correct |
37 ms |
47432 KB |
Output is correct |
9 |
Correct |
40 ms |
47440 KB |
Output is correct |
10 |
Correct |
40 ms |
47472 KB |
Output is correct |
11 |
Correct |
306 ms |
74824 KB |
Output is correct |
12 |
Correct |
351 ms |
74728 KB |
Output is correct |
13 |
Correct |
350 ms |
74820 KB |
Output is correct |
14 |
Correct |
288 ms |
74828 KB |
Output is correct |
15 |
Correct |
297 ms |
74968 KB |
Output is correct |
16 |
Correct |
301 ms |
74320 KB |
Output is correct |
17 |
Correct |
368 ms |
74280 KB |
Output is correct |
18 |
Correct |
355 ms |
74060 KB |
Output is correct |
19 |
Correct |
407 ms |
74600 KB |
Output is correct |
20 |
Correct |
224 ms |
70360 KB |
Output is correct |
21 |
Correct |
141 ms |
67888 KB |
Output is correct |
22 |
Correct |
138 ms |
66236 KB |
Output is correct |
23 |
Correct |
223 ms |
70360 KB |
Output is correct |
24 |
Correct |
191 ms |
70352 KB |
Output is correct |
25 |
Correct |
205 ms |
69704 KB |
Output is correct |
26 |
Correct |
203 ms |
69416 KB |
Output is correct |
27 |
Correct |
215 ms |
69464 KB |
Output is correct |
28 |
Correct |
199 ms |
69960 KB |
Output is correct |
29 |
Correct |
1045 ms |
133456 KB |
Output is correct |
30 |
Correct |
875 ms |
127564 KB |
Output is correct |
31 |
Correct |
1035 ms |
123448 KB |
Output is correct |
32 |
Correct |
1093 ms |
133452 KB |
Output is correct |
33 |
Correct |
1141 ms |
133452 KB |
Output is correct |
34 |
Correct |
1198 ms |
131148 KB |
Output is correct |
35 |
Correct |
1087 ms |
130836 KB |
Output is correct |
36 |
Correct |
1217 ms |
130840 KB |
Output is correct |
37 |
Correct |
1056 ms |
132192 KB |
Output is correct |
38 |
Correct |
773 ms |
135988 KB |
Output is correct |
39 |
Correct |
827 ms |
136068 KB |
Output is correct |
40 |
Correct |
818 ms |
132604 KB |
Output is correct |
41 |
Correct |
747 ms |
132208 KB |
Output is correct |
42 |
Correct |
817 ms |
132188 KB |
Output is correct |
43 |
Correct |
822 ms |
133964 KB |
Output is correct |
44 |
Correct |
851 ms |
136412 KB |
Output is correct |
45 |
Correct |
799 ms |
136268 KB |
Output is correct |
46 |
Correct |
788 ms |
133240 KB |
Output is correct |
47 |
Correct |
798 ms |
132648 KB |
Output is correct |
48 |
Correct |
846 ms |
132684 KB |
Output is correct |
49 |
Correct |
796 ms |
134788 KB |
Output is correct |
50 |
Correct |
961 ms |
136512 KB |
Output is correct |
51 |
Correct |
940 ms |
136524 KB |
Output is correct |
52 |
Correct |
950 ms |
134132 KB |
Output is correct |
53 |
Correct |
811 ms |
133756 KB |
Output is correct |
54 |
Correct |
862 ms |
133664 KB |
Output is correct |
55 |
Correct |
973 ms |
135120 KB |
Output is correct |