#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define pii pair<int, pi>
#define f first
#define s second
struct segTree{
int l, r;
segTree *left, *right;
pii val = {0, {0, 0}};
segTree(int a, int b){
l = a;
r = b;
if(l != r){
int mid = (l + r) / 2;
left = new segTree(l, mid);
right = new segTree(mid + 1, r);
}
}
void add(int x, pi v){
if(x < l || r < x) return;
if(l == r){
val.s.f = max(val.s.f, v.f);
val.s.s = max(val.s.s, v.s);
val.f = val.s.f + val.s.s;
return;
}
left->add(x, v);
right->add(x, v);
val.f = max(left->val.s.f + right->val.s.s, max(left->val.f, right->val.f));
val.s.f = max(left->val.s.f, right->val.s.f);
val.s.s = max(left->val.s.s, right->val.s.s);
}
pii qry(int a, int b){
if(r < a || b < l) return {0, {0, 0}};
if(a <= l && r <= b) return val;
pii lq = left->qry(a, b), rq = right->qry(a, b);
return {max(lq.s.f + rq.s.s, max(lq.f, rq.f)), {max(lq.s.f, rq.s.f), max(lq.s.s, rq.s.s)}};
}
};
const int maxn = 500000;
int n, q;
int a[maxn], b[maxn];
vector<int> p[maxn];
vector<pi> qry[maxn];
int ans[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
stack<int> stk;
for(int i = 0; i < n; i++){
cin >> a[i];
while(!stk.empty()){
int j = stk.top(), k = 2 * i - j;
if(k < n) p[j].push_back(k);
if(a[j] <= a[i]) stk.pop();
else break;
}
stk.push(i);
}
cin >> q;
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
l--, r--;
qry[l].push_back({r, i});
}
segTree tre(0, n);
for(int i = n - 1; i >= 0; i--){
tre.add(i, {0, a[i]});
for(int j : p[i]) tre.add(j, {a[i] + a[(i + j) / 2], 0});
for(pi j : qry[i]) ans[j.s] = tre.qry(i, j.f).f;
}
for(int i = 0; i < q; i++) cout << ans[i] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23928 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23928 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
312 ms |
42104 KB |
Output is correct |
12 |
Correct |
305 ms |
41976 KB |
Output is correct |
13 |
Correct |
300 ms |
42040 KB |
Output is correct |
14 |
Correct |
318 ms |
42204 KB |
Output is correct |
15 |
Correct |
318 ms |
42104 KB |
Output is correct |
16 |
Correct |
315 ms |
41464 KB |
Output is correct |
17 |
Correct |
299 ms |
41336 KB |
Output is correct |
18 |
Correct |
305 ms |
41336 KB |
Output is correct |
19 |
Correct |
315 ms |
42056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
51576 KB |
Output is correct |
2 |
Correct |
144 ms |
51492 KB |
Output is correct |
3 |
Correct |
146 ms |
52216 KB |
Output is correct |
4 |
Correct |
219 ms |
51676 KB |
Output is correct |
5 |
Correct |
201 ms |
51756 KB |
Output is correct |
6 |
Correct |
199 ms |
50936 KB |
Output is correct |
7 |
Correct |
192 ms |
50808 KB |
Output is correct |
8 |
Correct |
194 ms |
51064 KB |
Output is correct |
9 |
Correct |
193 ms |
51196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23928 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
312 ms |
42104 KB |
Output is correct |
12 |
Correct |
305 ms |
41976 KB |
Output is correct |
13 |
Correct |
300 ms |
42040 KB |
Output is correct |
14 |
Correct |
318 ms |
42204 KB |
Output is correct |
15 |
Correct |
318 ms |
42104 KB |
Output is correct |
16 |
Correct |
315 ms |
41464 KB |
Output is correct |
17 |
Correct |
299 ms |
41336 KB |
Output is correct |
18 |
Correct |
305 ms |
41336 KB |
Output is correct |
19 |
Correct |
315 ms |
42056 KB |
Output is correct |
20 |
Correct |
199 ms |
51576 KB |
Output is correct |
21 |
Correct |
144 ms |
51492 KB |
Output is correct |
22 |
Correct |
146 ms |
52216 KB |
Output is correct |
23 |
Correct |
219 ms |
51676 KB |
Output is correct |
24 |
Correct |
201 ms |
51756 KB |
Output is correct |
25 |
Correct |
199 ms |
50936 KB |
Output is correct |
26 |
Correct |
192 ms |
50808 KB |
Output is correct |
27 |
Correct |
194 ms |
51064 KB |
Output is correct |
28 |
Correct |
193 ms |
51196 KB |
Output is correct |
29 |
Correct |
1349 ms |
116832 KB |
Output is correct |
30 |
Correct |
1210 ms |
116352 KB |
Output is correct |
31 |
Correct |
1182 ms |
118208 KB |
Output is correct |
32 |
Correct |
1330 ms |
116856 KB |
Output is correct |
33 |
Correct |
1344 ms |
116824 KB |
Output is correct |
34 |
Correct |
1313 ms |
114664 KB |
Output is correct |
35 |
Correct |
1308 ms |
114424 KB |
Output is correct |
36 |
Correct |
1319 ms |
114332 KB |
Output is correct |
37 |
Correct |
1312 ms |
115808 KB |
Output is correct |
38 |
Correct |
842 ms |
122492 KB |
Output is correct |
39 |
Correct |
846 ms |
122616 KB |
Output is correct |
40 |
Correct |
812 ms |
119160 KB |
Output is correct |
41 |
Correct |
832 ms |
118776 KB |
Output is correct |
42 |
Correct |
821 ms |
118648 KB |
Output is correct |
43 |
Correct |
862 ms |
120568 KB |
Output is correct |
44 |
Correct |
912 ms |
122232 KB |
Output is correct |
45 |
Correct |
923 ms |
122104 KB |
Output is correct |
46 |
Correct |
893 ms |
119032 KB |
Output is correct |
47 |
Correct |
876 ms |
118392 KB |
Output is correct |
48 |
Correct |
912 ms |
118392 KB |
Output is correct |
49 |
Correct |
873 ms |
120440 KB |
Output is correct |
50 |
Correct |
992 ms |
120056 KB |
Output is correct |
51 |
Correct |
996 ms |
120056 KB |
Output is correct |
52 |
Correct |
991 ms |
117624 KB |
Output is correct |
53 |
Correct |
999 ms |
117368 KB |
Output is correct |
54 |
Correct |
963 ms |
117260 KB |
Output is correct |
55 |
Correct |
976 ms |
119032 KB |
Output is correct |