#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
const int N = 5e5 + 5;
struct ST{
int tree[N * 4], add[N * 4], p[N * 4];
void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
if(lx == rx) { add[x] = v; return; }
int m = (lx + rx) >> 1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
add[x] = max(add[x<<1], add[x<<1|1]);
}
void push(int x, int lx, int rx){
if(lx == rx || !p[x]) return;
tree[x<<1] = max(tree[x<<1], p[x] + add[x<<1]);
tree[x<<1|1] = max(tree[x<<1|1], p[x] + add[x<<1|1]);
p[x<<1] = max(p[x<<1], p[x]), p[x<<1|1] = max(p[x<<1|1], p[x]);
p[x] = 0;
}
void umax(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
tree[x] = max(tree[x], v + add[x]);
p[x] = max(p[x], v); return;
} push(x, lx, rx); int m = (lx + rx) >> 1;
umax(l, r, v, lx, m, x<<1), umax(l, r, v, m+1, rx, x<<1|1);
tree[x] = max(tree[x<<1], tree[x<<1|1]);
}
int get(int l, int r, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return 0;
if(lx >= l && rx <= r) return tree[x];
push(x, lx, rx);
int m = (lx + rx) >> 1;
return max(get(l, r, lx, m, x<<1), get(l, r, m+1, rx, x<<1|1));
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
vector<vector<ar<int, 2>>> qq(n);
int q; cin>>q;
for(int i=0;i<q;i++){
int l, r; cin>>l>>r; l--, r--;
qq[l].push_back({r, i});
}
for(int i=0;i<n;i++) tree.sett(i, a[i]);
vector<int> ss;
vector<vector<int>> pp(n);
for(int i=0;i<n;i++){
while(!ss.empty() && a[ss.back()] < a[i]) ss.pop_back();
if(!ss.empty()) pp[ss.back()].push_back(i);
ss.push_back(i);
} ss.clear();
for(int i=n-1;~i;i--){
while(!ss.empty() && a[ss.back()] < a[i]) ss.pop_back();
if(!ss.empty()) pp[i].push_back(ss.back());
ss.push_back(i);
}
vector<int> res(q);
for(int i=n-1;~i;i--){
for(auto j : pp[i]){
if(j + j - i < n) tree.umax(j + j - i, n - 1, a[i] + a[j]);
}
for(auto x : qq[i]){
res[x[1]] = tree.get(i, x[0]);
}
}
for(int i=0;i<q;i++) cout<<res[i]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
576 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
576 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
281 ms |
28052 KB |
Output is correct |
12 |
Correct |
269 ms |
28128 KB |
Output is correct |
13 |
Correct |
298 ms |
28116 KB |
Output is correct |
14 |
Correct |
258 ms |
28012 KB |
Output is correct |
15 |
Correct |
264 ms |
28132 KB |
Output is correct |
16 |
Correct |
251 ms |
27520 KB |
Output is correct |
17 |
Correct |
326 ms |
27456 KB |
Output is correct |
18 |
Correct |
280 ms |
27164 KB |
Output is correct |
19 |
Correct |
243 ms |
27880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
30272 KB |
Output is correct |
2 |
Correct |
139 ms |
30592 KB |
Output is correct |
3 |
Correct |
133 ms |
30764 KB |
Output is correct |
4 |
Correct |
209 ms |
30308 KB |
Output is correct |
5 |
Correct |
253 ms |
30220 KB |
Output is correct |
6 |
Correct |
220 ms |
29744 KB |
Output is correct |
7 |
Correct |
208 ms |
29632 KB |
Output is correct |
8 |
Correct |
227 ms |
29632 KB |
Output is correct |
9 |
Correct |
208 ms |
30148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
576 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
281 ms |
28052 KB |
Output is correct |
12 |
Correct |
269 ms |
28128 KB |
Output is correct |
13 |
Correct |
298 ms |
28116 KB |
Output is correct |
14 |
Correct |
258 ms |
28012 KB |
Output is correct |
15 |
Correct |
264 ms |
28132 KB |
Output is correct |
16 |
Correct |
251 ms |
27520 KB |
Output is correct |
17 |
Correct |
326 ms |
27456 KB |
Output is correct |
18 |
Correct |
280 ms |
27164 KB |
Output is correct |
19 |
Correct |
243 ms |
27880 KB |
Output is correct |
20 |
Correct |
210 ms |
30272 KB |
Output is correct |
21 |
Correct |
139 ms |
30592 KB |
Output is correct |
22 |
Correct |
133 ms |
30764 KB |
Output is correct |
23 |
Correct |
209 ms |
30308 KB |
Output is correct |
24 |
Correct |
253 ms |
30220 KB |
Output is correct |
25 |
Correct |
220 ms |
29744 KB |
Output is correct |
26 |
Correct |
208 ms |
29632 KB |
Output is correct |
27 |
Correct |
227 ms |
29632 KB |
Output is correct |
28 |
Correct |
208 ms |
30148 KB |
Output is correct |
29 |
Correct |
1203 ms |
103980 KB |
Output is correct |
30 |
Correct |
1002 ms |
104836 KB |
Output is correct |
31 |
Correct |
970 ms |
104884 KB |
Output is correct |
32 |
Correct |
1172 ms |
104068 KB |
Output is correct |
33 |
Correct |
1199 ms |
104028 KB |
Output is correct |
34 |
Correct |
1236 ms |
101620 KB |
Output is correct |
35 |
Correct |
1298 ms |
101420 KB |
Output is correct |
36 |
Correct |
1185 ms |
101388 KB |
Output is correct |
37 |
Correct |
1195 ms |
102840 KB |
Output is correct |
38 |
Correct |
959 ms |
106536 KB |
Output is correct |
39 |
Correct |
996 ms |
106564 KB |
Output is correct |
40 |
Correct |
995 ms |
103144 KB |
Output is correct |
41 |
Correct |
1010 ms |
102680 KB |
Output is correct |
42 |
Correct |
1058 ms |
102724 KB |
Output is correct |
43 |
Correct |
904 ms |
104564 KB |
Output is correct |
44 |
Correct |
988 ms |
106376 KB |
Output is correct |
45 |
Correct |
999 ms |
106244 KB |
Output is correct |
46 |
Correct |
909 ms |
103256 KB |
Output is correct |
47 |
Correct |
1029 ms |
102748 KB |
Output is correct |
48 |
Correct |
973 ms |
102788 KB |
Output is correct |
49 |
Correct |
967 ms |
104840 KB |
Output is correct |
50 |
Correct |
1037 ms |
105584 KB |
Output is correct |
51 |
Correct |
1047 ms |
105600 KB |
Output is correct |
52 |
Correct |
1056 ms |
102976 KB |
Output is correct |
53 |
Correct |
1031 ms |
102740 KB |
Output is correct |
54 |
Correct |
1039 ms |
102792 KB |
Output is correct |
55 |
Correct |
1024 ms |
104388 KB |
Output is correct |