#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int M = 1 << 19, N = 2 * M, INF = 1e9 + 42;
struct Node {
int maxAB = -INF, maxABC = -INF, tagC = -INF;
};
Node node[N];
void applyOp(int i, int newC) {
node[i].tagC = max(node[i].tagC, newC);
node[i].maxABC = max(node[i].maxABC, node[i].maxAB + newC);
}
void propage(int i) {
applyOp(i*2, node[i].tagC);
applyOp(i*2+1, node[i].tagC);
node[i].tagC = -INF;
}
void insertAB(int i, int deb, int fin, int pos, int ab) {
if(pos+1 <= deb || fin <= pos) return;
if(fin - deb == 1) {
node[i].maxAB = max(node[i].maxAB, ab);
return;
}
propage(i);
int mid = ((deb + fin) >> 1);
insertAB(i*2, deb, mid, pos, ab);
insertAB(i*2+1, mid, fin, pos, ab);
node[i].maxAB = max(node[i*2].maxAB, node[i*2+1].maxAB);
}
int update(int i, int deb, int fin, int l, int r, int newC) {
if(fin <= l || r <= deb) return -INF;
if(l <= deb && fin <= r) {
applyOp(i, newC);
return node[i].maxABC;
}
propage(i);
int mid = ((deb + fin) >> 1),
ans = max(update(i*2, deb, mid, l, r, newC),
update(i*2+1, mid, fin, l, r, newC));
node[i].maxABC = max(node[i*2].maxABC, node[i*2+1].maxABC);
return ans;
}
vector<int> req[M];
vector<pair<int, int>> ab[N];
int n, q, a[N], deb[M], ans[M];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
deque<int> imax;
for(int i = 0; i < n; i++) {
while(!imax.empty() && a[imax[0]] <= a[i]) {
ab[2*i - imax[0]].push_back({imax[0], a[imax[0]] + a[i]});
imax.pop_front();
}
imax.push_front(i);
}
imax.clear();
for(int i = n-1; i > -1; i--) {
while(!imax.empty() && a[imax[0]] < a[i]) {
ab[2 * imax[0] - i].push_back({i, a[imax[0]] + a[i]});
imax.pop_front();
}
imax.push_front(i);
}
cin >> q;
for(int i = 0; i < q; i++) {
int fin; cin >> deb[i] >> fin;
deb[i]--, fin--;
req[fin].push_back(i);
}
for(int i = 0; i < n; i++) {
for(auto pii : ab[i])
insertAB(1, 0, M, pii.first, pii.second);
update(1, 0, M, 0, i, a[i]);
for(int id : req[i])
ans[id] = update(1, 0, M, deb[id], i+1, -INF);
}
for(int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
37332 KB |
Output is correct |
2 |
Correct |
17 ms |
37336 KB |
Output is correct |
3 |
Correct |
19 ms |
37332 KB |
Output is correct |
4 |
Correct |
16 ms |
37356 KB |
Output is correct |
5 |
Correct |
17 ms |
37340 KB |
Output is correct |
6 |
Correct |
16 ms |
37348 KB |
Output is correct |
7 |
Correct |
16 ms |
37332 KB |
Output is correct |
8 |
Correct |
17 ms |
37236 KB |
Output is correct |
9 |
Correct |
16 ms |
37332 KB |
Output is correct |
10 |
Correct |
19 ms |
37280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
37332 KB |
Output is correct |
2 |
Correct |
17 ms |
37336 KB |
Output is correct |
3 |
Correct |
19 ms |
37332 KB |
Output is correct |
4 |
Correct |
16 ms |
37356 KB |
Output is correct |
5 |
Correct |
17 ms |
37340 KB |
Output is correct |
6 |
Correct |
16 ms |
37348 KB |
Output is correct |
7 |
Correct |
16 ms |
37332 KB |
Output is correct |
8 |
Correct |
17 ms |
37236 KB |
Output is correct |
9 |
Correct |
16 ms |
37332 KB |
Output is correct |
10 |
Correct |
19 ms |
37280 KB |
Output is correct |
11 |
Correct |
303 ms |
50608 KB |
Output is correct |
12 |
Correct |
322 ms |
51172 KB |
Output is correct |
13 |
Correct |
346 ms |
51032 KB |
Output is correct |
14 |
Correct |
343 ms |
51116 KB |
Output is correct |
15 |
Correct |
320 ms |
51184 KB |
Output is correct |
16 |
Correct |
367 ms |
50548 KB |
Output is correct |
17 |
Correct |
352 ms |
50544 KB |
Output is correct |
18 |
Correct |
309 ms |
50540 KB |
Output is correct |
19 |
Correct |
430 ms |
51048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
49828 KB |
Output is correct |
2 |
Correct |
150 ms |
50080 KB |
Output is correct |
3 |
Correct |
143 ms |
49320 KB |
Output is correct |
4 |
Correct |
192 ms |
50020 KB |
Output is correct |
5 |
Correct |
232 ms |
50028 KB |
Output is correct |
6 |
Correct |
193 ms |
50816 KB |
Output is correct |
7 |
Correct |
204 ms |
50720 KB |
Output is correct |
8 |
Correct |
203 ms |
50724 KB |
Output is correct |
9 |
Correct |
205 ms |
51104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
37332 KB |
Output is correct |
2 |
Correct |
17 ms |
37336 KB |
Output is correct |
3 |
Correct |
19 ms |
37332 KB |
Output is correct |
4 |
Correct |
16 ms |
37356 KB |
Output is correct |
5 |
Correct |
17 ms |
37340 KB |
Output is correct |
6 |
Correct |
16 ms |
37348 KB |
Output is correct |
7 |
Correct |
16 ms |
37332 KB |
Output is correct |
8 |
Correct |
17 ms |
37236 KB |
Output is correct |
9 |
Correct |
16 ms |
37332 KB |
Output is correct |
10 |
Correct |
19 ms |
37280 KB |
Output is correct |
11 |
Correct |
303 ms |
50608 KB |
Output is correct |
12 |
Correct |
322 ms |
51172 KB |
Output is correct |
13 |
Correct |
346 ms |
51032 KB |
Output is correct |
14 |
Correct |
343 ms |
51116 KB |
Output is correct |
15 |
Correct |
320 ms |
51184 KB |
Output is correct |
16 |
Correct |
367 ms |
50548 KB |
Output is correct |
17 |
Correct |
352 ms |
50544 KB |
Output is correct |
18 |
Correct |
309 ms |
50540 KB |
Output is correct |
19 |
Correct |
430 ms |
51048 KB |
Output is correct |
20 |
Correct |
202 ms |
49828 KB |
Output is correct |
21 |
Correct |
150 ms |
50080 KB |
Output is correct |
22 |
Correct |
143 ms |
49320 KB |
Output is correct |
23 |
Correct |
192 ms |
50020 KB |
Output is correct |
24 |
Correct |
232 ms |
50028 KB |
Output is correct |
25 |
Correct |
193 ms |
50816 KB |
Output is correct |
26 |
Correct |
204 ms |
50720 KB |
Output is correct |
27 |
Correct |
203 ms |
50724 KB |
Output is correct |
28 |
Correct |
205 ms |
51104 KB |
Output is correct |
29 |
Correct |
1204 ms |
97380 KB |
Output is correct |
30 |
Correct |
951 ms |
97492 KB |
Output is correct |
31 |
Correct |
949 ms |
95368 KB |
Output is correct |
32 |
Correct |
1071 ms |
97308 KB |
Output is correct |
33 |
Correct |
1120 ms |
97428 KB |
Output is correct |
34 |
Correct |
962 ms |
95012 KB |
Output is correct |
35 |
Correct |
1037 ms |
94840 KB |
Output is correct |
36 |
Correct |
1002 ms |
94844 KB |
Output is correct |
37 |
Correct |
1012 ms |
96268 KB |
Output is correct |
38 |
Correct |
830 ms |
104000 KB |
Output is correct |
39 |
Correct |
791 ms |
104028 KB |
Output is correct |
40 |
Correct |
730 ms |
100648 KB |
Output is correct |
41 |
Correct |
714 ms |
100096 KB |
Output is correct |
42 |
Correct |
834 ms |
100136 KB |
Output is correct |
43 |
Correct |
726 ms |
101912 KB |
Output is correct |
44 |
Correct |
764 ms |
103308 KB |
Output is correct |
45 |
Correct |
781 ms |
103412 KB |
Output is correct |
46 |
Correct |
765 ms |
100416 KB |
Output is correct |
47 |
Correct |
761 ms |
99812 KB |
Output is correct |
48 |
Correct |
749 ms |
99736 KB |
Output is correct |
49 |
Correct |
748 ms |
101900 KB |
Output is correct |
50 |
Correct |
898 ms |
101160 KB |
Output is correct |
51 |
Correct |
818 ms |
101136 KB |
Output is correct |
52 |
Correct |
820 ms |
98636 KB |
Output is correct |
53 |
Correct |
804 ms |
98396 KB |
Output is correct |
54 |
Correct |
801 ms |
98432 KB |
Output is correct |
55 |
Correct |
878 ms |
100020 KB |
Output is correct |