#include <bits/stdc++.h>
using namespace std;
mt19937 rng(time(0));
int random(int l, int r) { return rng() % (r - l + 1) + l; }
struct query {
int l, r;
int ans;
};
struct node {
int mx;
int ans;
int lzy;
};
const int N = 5e5 + 10;
int n, q;
int a[N];
query Q[N];
node st[4 * N];
vector<int> qs[N];
vector<int> upd[N];
stack<int> stk;
void build(int index, int l, int r) {
if(l > r) return;
if(l == r) {
st[index].mx = a[l];
st[index].ans = a[l];
st[index].lzy = 0;
return;
}
int mid = (l + r) / 2;
build(2 * index, l, mid);
build(2 * index + 1, mid + 1, r);
st[index].mx = max(st[2 * index].mx, st[2 * index + 1].mx);
st[index].ans = max(st[2 * index].ans, st[2 * index + 1].ans);
st[index].lzy = 0;
}
void propagate(int index) {
if(2 * index + 1 < 4 * N && st[index].lzy > 0) {
st[2 * index].ans = max(st[2 * index].ans, st[2 * index].mx + st[index].lzy);
st[2 * index].lzy = max(st[2 * index].lzy, st[index].lzy);
st[2 * index + 1].ans = max(st[2 * index + 1].ans, st[2 * index + 1].mx + st[index].lzy);
st[2 * index + 1].lzy = max(st[2 * index + 1].lzy, st[index].lzy);
st[index].lzy = 0;
}
}
void update(int index, int l, int r, int L, int R, int val) {
if(l > r || r < L || R < l) return;
if(L <= l && r <= R) {
st[index].ans = max(st[index].ans, val + st[index].mx);
st[index].lzy = max(st[index].lzy, val);
return;
}
propagate(index);
int mid = (l + r) / 2;
update(2 * index, l, mid, L, R, val);
update(2 * index + 1, mid + 1, r, L, R, val);
st[index].ans = max(st[2 * index].ans, st[2 * index + 1].ans);
}
int get(int index, int l, int r, int L, int R) {
if(l > r || r < L || R < l) return 0;
if(L <= l && r <= R) return st[index].ans;
propagate(index);
int mid = (l + r) / 2;
return max(get(2 * index, l, mid, L, R), get(2 * index + 1, mid + 1, r, L, R));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// while(true) {
cin >> n;
// n = 10;
for(int i = 1; i <= n; i++) cin >> a[i];
// for(int i = 1; i <= n; i++) a[i] = random(1, 10);
cin >> q;
// q = 10;
for(int i = 1; i <= q; i++) {
cin >> Q[i].l >> Q[i].r;
// Q[i].l = random(1, 8);
// Q[i].r = random(Q[i].l + 2, 10);
qs[Q[i].l].push_back(i);
}
for(int i = 1; i <= n; i++) {
while(!stk.empty() && a[stk.top()] < a[i]) stk.pop();
if(!stk.empty()) {
int x = stk.top(), y = i;
if(y + (y - x) <= n) upd[x].push_back(y);
}
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i >= 1; i--) {
while(!stk.empty() && a[stk.top()] < a[i]) stk.pop();
if(!stk.empty()) {
int x = i, y = stk.top();
if(y + (y - x) <= n) upd[x].push_back(y);
}
stk.push(i);
}
build(1, 1, n);
for(int l = n; l >= 1; l--) {
for(int y : upd[l]) {
int x = l;
int z = y + (y - x);
update(1, 1, n, z, n, a[x] + a[y]);
}
for(int ind : qs[l]) {
int r = Q[ind].r;
Q[ind].ans = get(1, 1, n, l, r);
}
}
for(int i = 1; i <= q; i++) cout << Q[i].ans << "\n";
// for(int i = 1; i <= q; i++) {
// int l = Q[i].l, r = Q[i].r;
// int mx = 0;
// for(int x = l; x <= r; x++) {
// for(int y = x + 1; y <= r; y++) {
// for(int z = y + (y - x); z <= r; z++) {
// mx = max(mx, a[x] + a[y] + a[z]);
// }
// }
// }
// if(mx != Q[i].ans) {
// cout << n << "\n";
// for(int i = 1; i <= n; i++) cout << a[i] << " ";
// cout << "\n";
// cout << q << "\n";
// for(int i = 1; i <= q; i++) cout << Q[i].l << " " << Q[i].r << "\n";
// return 0;
// }
// }
// }
return 0;
}
/*
10
2 4 5 2 10 2 9 9 10 8
10
8 10
5 7
7 9
5 10
3 10
8 10
5 9
3 5
8 10
7 9
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29272 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29376 KB |
Output is correct |
9 |
Correct |
7 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29272 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29376 KB |
Output is correct |
9 |
Correct |
7 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
216 ms |
46676 KB |
Output is correct |
12 |
Correct |
218 ms |
46416 KB |
Output is correct |
13 |
Correct |
238 ms |
46852 KB |
Output is correct |
14 |
Correct |
218 ms |
46676 KB |
Output is correct |
15 |
Correct |
232 ms |
46800 KB |
Output is correct |
16 |
Correct |
220 ms |
45904 KB |
Output is correct |
17 |
Correct |
220 ms |
45908 KB |
Output is correct |
18 |
Correct |
224 ms |
45868 KB |
Output is correct |
19 |
Correct |
237 ms |
46532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
43828 KB |
Output is correct |
2 |
Correct |
64 ms |
44328 KB |
Output is correct |
3 |
Correct |
65 ms |
44368 KB |
Output is correct |
4 |
Correct |
111 ms |
43604 KB |
Output is correct |
5 |
Correct |
110 ms |
43576 KB |
Output is correct |
6 |
Correct |
108 ms |
43168 KB |
Output is correct |
7 |
Correct |
108 ms |
43056 KB |
Output is correct |
8 |
Correct |
108 ms |
43060 KB |
Output is correct |
9 |
Correct |
110 ms |
43256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29272 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29376 KB |
Output is correct |
9 |
Correct |
7 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
216 ms |
46676 KB |
Output is correct |
12 |
Correct |
218 ms |
46416 KB |
Output is correct |
13 |
Correct |
238 ms |
46852 KB |
Output is correct |
14 |
Correct |
218 ms |
46676 KB |
Output is correct |
15 |
Correct |
232 ms |
46800 KB |
Output is correct |
16 |
Correct |
220 ms |
45904 KB |
Output is correct |
17 |
Correct |
220 ms |
45908 KB |
Output is correct |
18 |
Correct |
224 ms |
45868 KB |
Output is correct |
19 |
Correct |
237 ms |
46532 KB |
Output is correct |
20 |
Correct |
110 ms |
43828 KB |
Output is correct |
21 |
Correct |
64 ms |
44328 KB |
Output is correct |
22 |
Correct |
65 ms |
44368 KB |
Output is correct |
23 |
Correct |
111 ms |
43604 KB |
Output is correct |
24 |
Correct |
110 ms |
43576 KB |
Output is correct |
25 |
Correct |
108 ms |
43168 KB |
Output is correct |
26 |
Correct |
108 ms |
43056 KB |
Output is correct |
27 |
Correct |
108 ms |
43060 KB |
Output is correct |
28 |
Correct |
110 ms |
43256 KB |
Output is correct |
29 |
Correct |
754 ms |
88148 KB |
Output is correct |
30 |
Correct |
619 ms |
89412 KB |
Output is correct |
31 |
Correct |
626 ms |
89544 KB |
Output is correct |
32 |
Correct |
765 ms |
88024 KB |
Output is correct |
33 |
Correct |
762 ms |
88148 KB |
Output is correct |
34 |
Correct |
762 ms |
85844 KB |
Output is correct |
35 |
Correct |
761 ms |
85376 KB |
Output is correct |
36 |
Correct |
752 ms |
85380 KB |
Output is correct |
37 |
Correct |
786 ms |
86984 KB |
Output is correct |
38 |
Correct |
551 ms |
94628 KB |
Output is correct |
39 |
Correct |
532 ms |
94648 KB |
Output is correct |
40 |
Correct |
525 ms |
91476 KB |
Output is correct |
41 |
Correct |
517 ms |
90980 KB |
Output is correct |
42 |
Correct |
525 ms |
90968 KB |
Output is correct |
43 |
Correct |
528 ms |
92756 KB |
Output is correct |
44 |
Correct |
572 ms |
94032 KB |
Output is correct |
45 |
Correct |
565 ms |
94092 KB |
Output is correct |
46 |
Correct |
551 ms |
91004 KB |
Output is correct |
47 |
Correct |
553 ms |
90400 KB |
Output is correct |
48 |
Correct |
556 ms |
90648 KB |
Output is correct |
49 |
Correct |
570 ms |
92752 KB |
Output is correct |
50 |
Correct |
648 ms |
91988 KB |
Output is correct |
51 |
Correct |
627 ms |
91772 KB |
Output is correct |
52 |
Correct |
612 ms |
89428 KB |
Output is correct |
53 |
Correct |
607 ms |
89116 KB |
Output is correct |
54 |
Correct |
594 ms |
89108 KB |
Output is correct |
55 |
Correct |
609 ms |
90704 KB |
Output is correct |