#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 500005;
constexpr int MAXINT = 1073741823;
vector<int> pairs[MAXN];
vector<pair<int, int>> queries[MAXN];
stack<int> s;
int a[MAXN], answer[MAXN];
struct node {
int result = 0, maximum_pair = 0, maximum_a = 0;
} segment[MAXN * 4];
node merge(node x, node y) {
node result;
result.maximum_a = max(x.maximum_a, y.maximum_a);
result.maximum_pair = max(x.maximum_pair, y.maximum_pair);
result.result = max(x.maximum_pair + y.maximum_a, max(x.result, y.result));
return result;
}
node get_answer(int from, int to, int current, int beginning, int ending) {
node nothing;
if (from > ending || to < beginning)
return nothing;
else if (beginning <= from && to <= ending)
return segment[current];
int mid = (from + to) / 2;
return merge(get_answer(from, mid, current * 2 + 1, beginning, ending), get_answer(mid + 1, to, current * 2 + 2, beginning, ending));
}
void update(int from, int to, int current, int index, int value, bool is_pair) {
if (from > index || to < index)
return;
else if (from == to) {
if (is_pair)
segment[current].maximum_pair = max(segment[current].maximum_pair, value);
else
segment[current].maximum_a = max(segment[current].maximum_a, value);
segment[current].result = max(segment[current].result, segment[current].maximum_a + segment[current].maximum_pair);
return;
}
int mid = (from + to) / 2;
update(from, mid, current * 2 + 1, index, value, is_pair);
update(mid + 1, to, current * 2 + 2, index, value, is_pair);
segment[current] = merge(segment[current * 2 + 1], segment[current * 2 + 2]);
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, t, from, to;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
update(0, n - 1, 0, i, a[i], false);
while (!s.empty() && a[s.top()] <= a[i]) {
pairs[s.top()].push_back(i);
s.pop();
}
if (!s.empty())
pairs[s.top()].push_back(i);
s.push(i);
}
cin >> t;
for (int q = 0; q < t; q++) {
cin >> from >> to;
from--;
to--;
queries[from].emplace_back(to, q);
}
for (int i = n - 1; i >= 0; i--) {
for (int j : pairs[i])
if (2 * j - i < n)
update(0, n - 1, 0, 2 * j - i, a[i] + a[j], true);
for (pair<int, int> j : queries[i])
answer[j.second] = get_answer(0, n - 1, 0, i + 2, j.first).result;
}
for (int q = 0; q < t; q++)
cout << answer[q] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23704 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
13 ms |
23772 KB |
Output is correct |
9 |
Correct |
14 ms |
23712 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23704 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
13 ms |
23772 KB |
Output is correct |
9 |
Correct |
14 ms |
23712 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
11 |
Correct |
490 ms |
42792 KB |
Output is correct |
12 |
Correct |
438 ms |
42748 KB |
Output is correct |
13 |
Correct |
438 ms |
42768 KB |
Output is correct |
14 |
Correct |
460 ms |
42804 KB |
Output is correct |
15 |
Correct |
421 ms |
42764 KB |
Output is correct |
16 |
Correct |
429 ms |
42160 KB |
Output is correct |
17 |
Correct |
433 ms |
42032 KB |
Output is correct |
18 |
Correct |
454 ms |
42092 KB |
Output is correct |
19 |
Correct |
430 ms |
42656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
38956 KB |
Output is correct |
2 |
Correct |
233 ms |
38740 KB |
Output is correct |
3 |
Correct |
219 ms |
39560 KB |
Output is correct |
4 |
Correct |
311 ms |
38960 KB |
Output is correct |
5 |
Correct |
336 ms |
38980 KB |
Output is correct |
6 |
Correct |
309 ms |
38204 KB |
Output is correct |
7 |
Correct |
304 ms |
38200 KB |
Output is correct |
8 |
Correct |
312 ms |
38204 KB |
Output is correct |
9 |
Correct |
315 ms |
38524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23704 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23756 KB |
Output is correct |
8 |
Correct |
13 ms |
23772 KB |
Output is correct |
9 |
Correct |
14 ms |
23712 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
11 |
Correct |
490 ms |
42792 KB |
Output is correct |
12 |
Correct |
438 ms |
42748 KB |
Output is correct |
13 |
Correct |
438 ms |
42768 KB |
Output is correct |
14 |
Correct |
460 ms |
42804 KB |
Output is correct |
15 |
Correct |
421 ms |
42764 KB |
Output is correct |
16 |
Correct |
429 ms |
42160 KB |
Output is correct |
17 |
Correct |
433 ms |
42032 KB |
Output is correct |
18 |
Correct |
454 ms |
42092 KB |
Output is correct |
19 |
Correct |
430 ms |
42656 KB |
Output is correct |
20 |
Correct |
308 ms |
38956 KB |
Output is correct |
21 |
Correct |
233 ms |
38740 KB |
Output is correct |
22 |
Correct |
219 ms |
39560 KB |
Output is correct |
23 |
Correct |
311 ms |
38960 KB |
Output is correct |
24 |
Correct |
336 ms |
38980 KB |
Output is correct |
25 |
Correct |
309 ms |
38204 KB |
Output is correct |
26 |
Correct |
304 ms |
38200 KB |
Output is correct |
27 |
Correct |
312 ms |
38204 KB |
Output is correct |
28 |
Correct |
315 ms |
38524 KB |
Output is correct |
29 |
Correct |
1587 ms |
82212 KB |
Output is correct |
30 |
Correct |
1319 ms |
81432 KB |
Output is correct |
31 |
Correct |
1305 ms |
83492 KB |
Output is correct |
32 |
Correct |
1562 ms |
82076 KB |
Output is correct |
33 |
Correct |
1546 ms |
82108 KB |
Output is correct |
34 |
Correct |
1534 ms |
79972 KB |
Output is correct |
35 |
Correct |
1533 ms |
79560 KB |
Output is correct |
36 |
Correct |
1530 ms |
79536 KB |
Output is correct |
37 |
Correct |
1570 ms |
81088 KB |
Output is correct |
38 |
Correct |
1270 ms |
87796 KB |
Output is correct |
39 |
Correct |
1257 ms |
87896 KB |
Output is correct |
40 |
Correct |
1252 ms |
84492 KB |
Output is correct |
41 |
Correct |
1234 ms |
83988 KB |
Output is correct |
42 |
Correct |
1245 ms |
84080 KB |
Output is correct |
43 |
Correct |
1281 ms |
85656 KB |
Output is correct |
44 |
Correct |
1312 ms |
87092 KB |
Output is correct |
45 |
Correct |
1304 ms |
87204 KB |
Output is correct |
46 |
Correct |
1280 ms |
84128 KB |
Output is correct |
47 |
Correct |
1279 ms |
83564 KB |
Output is correct |
48 |
Correct |
1305 ms |
83752 KB |
Output is correct |
49 |
Correct |
1304 ms |
85644 KB |
Output is correct |
50 |
Correct |
1377 ms |
85252 KB |
Output is correct |
51 |
Correct |
1378 ms |
85252 KB |
Output is correct |
52 |
Correct |
1355 ms |
82804 KB |
Output is correct |
53 |
Correct |
1335 ms |
82472 KB |
Output is correct |
54 |
Correct |
1373 ms |
82516 KB |
Output is correct |
55 |
Correct |
1368 ms |
84188 KB |
Output is correct |