#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 500500;
int t[4 * N];
int mx[4 * N];
int d[4 * N];
void apply(int v, int val) {
d[v] = max(d[v], val);
t[v] = max(t[v], mx[v] + val);
}
void push(int v) {
apply(2 * v, d[v]);
apply(2 * v + 1, d[v]);
}
void upd(int v, int l, int r, int L, int R, int val) {
if (l >= R || r <= L) {
return;
}
if (l >= L && r <= R) {
apply(v, val);
return;
}
push(v);
int m = (l + r) / 2;
upd(2 * v, l, m, L, R, val);
upd(2 * v + 1, m, r, L, R, val);
t[v] = max({mx[v] + d[v], t[2 * v], t[2 * v + 1]});
}
int get(int v, int l, int r, int L, int R) {
if (l >= R || r <= L) {
return (int) -1e9;
}
if (l >= L && r <= R) {
return t[v];
}
push(v);
int m = (l + r) / 2;
return max(get(2 * v, l, m, L, R), get(2 * v + 1, m, r, L, R));
}
void build(int v, int l, int r, int a[N]) {
if (r - l == 1) {
t[v] = -1e9;
mx[v] = a[l];
d[v] = -1e9;
} else {
int m = (l + r) / 2;
build(2 * v, l, m, a);
build(2 * v + 1, m, r, a);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
d[v] = -1e9;
t[v] = -1e9;
}
}
int n;
int a[N];
vector<pair<int, int>> qr[N];
vector<int> e[N];
int q;
int ans[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cin >> q;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l;
qr[l].push_back({r, i});
}
vector<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty() && a[st.back()] <= a[i]) {
e[st.back()].push_back(i);
st.pop_back();
}
if (!st.empty()) {
e[st.back()].push_back(i);
}
st.push_back(i);
}
build(1, 0, n, a);
for (int i = n - 1; i >= 0; --i) {
for (int j : e[i]) {
upd(1, 0, n, i + (j - i) * 2, n, a[i] + a[j]);
}
for (auto qq : qr[i]) {
ans[qq.second] = get(1, 0, n, i, qq.first);
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23840 KB |
Output is correct |
2 |
Correct |
12 ms |
23840 KB |
Output is correct |
3 |
Correct |
13 ms |
23860 KB |
Output is correct |
4 |
Correct |
14 ms |
23780 KB |
Output is correct |
5 |
Correct |
13 ms |
23828 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23776 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23832 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23840 KB |
Output is correct |
2 |
Correct |
12 ms |
23840 KB |
Output is correct |
3 |
Correct |
13 ms |
23860 KB |
Output is correct |
4 |
Correct |
14 ms |
23780 KB |
Output is correct |
5 |
Correct |
13 ms |
23828 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23776 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23832 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
11 |
Correct |
321 ms |
42668 KB |
Output is correct |
12 |
Correct |
288 ms |
42512 KB |
Output is correct |
13 |
Correct |
293 ms |
42692 KB |
Output is correct |
14 |
Correct |
324 ms |
42648 KB |
Output is correct |
15 |
Correct |
286 ms |
42672 KB |
Output is correct |
16 |
Correct |
302 ms |
42028 KB |
Output is correct |
17 |
Correct |
290 ms |
41960 KB |
Output is correct |
18 |
Correct |
314 ms |
41916 KB |
Output is correct |
19 |
Correct |
289 ms |
42472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
38988 KB |
Output is correct |
2 |
Correct |
83 ms |
38788 KB |
Output is correct |
3 |
Correct |
83 ms |
39460 KB |
Output is correct |
4 |
Correct |
133 ms |
38940 KB |
Output is correct |
5 |
Correct |
134 ms |
38952 KB |
Output is correct |
6 |
Correct |
125 ms |
38248 KB |
Output is correct |
7 |
Correct |
135 ms |
38244 KB |
Output is correct |
8 |
Correct |
127 ms |
38244 KB |
Output is correct |
9 |
Correct |
131 ms |
38468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23840 KB |
Output is correct |
2 |
Correct |
12 ms |
23840 KB |
Output is correct |
3 |
Correct |
13 ms |
23860 KB |
Output is correct |
4 |
Correct |
14 ms |
23780 KB |
Output is correct |
5 |
Correct |
13 ms |
23828 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23776 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23832 KB |
Output is correct |
10 |
Correct |
13 ms |
23756 KB |
Output is correct |
11 |
Correct |
321 ms |
42668 KB |
Output is correct |
12 |
Correct |
288 ms |
42512 KB |
Output is correct |
13 |
Correct |
293 ms |
42692 KB |
Output is correct |
14 |
Correct |
324 ms |
42648 KB |
Output is correct |
15 |
Correct |
286 ms |
42672 KB |
Output is correct |
16 |
Correct |
302 ms |
42028 KB |
Output is correct |
17 |
Correct |
290 ms |
41960 KB |
Output is correct |
18 |
Correct |
314 ms |
41916 KB |
Output is correct |
19 |
Correct |
289 ms |
42472 KB |
Output is correct |
20 |
Correct |
133 ms |
38988 KB |
Output is correct |
21 |
Correct |
83 ms |
38788 KB |
Output is correct |
22 |
Correct |
83 ms |
39460 KB |
Output is correct |
23 |
Correct |
133 ms |
38940 KB |
Output is correct |
24 |
Correct |
134 ms |
38952 KB |
Output is correct |
25 |
Correct |
125 ms |
38248 KB |
Output is correct |
26 |
Correct |
135 ms |
38244 KB |
Output is correct |
27 |
Correct |
127 ms |
38244 KB |
Output is correct |
28 |
Correct |
131 ms |
38468 KB |
Output is correct |
29 |
Correct |
896 ms |
82196 KB |
Output is correct |
30 |
Correct |
836 ms |
81508 KB |
Output is correct |
31 |
Correct |
735 ms |
83456 KB |
Output is correct |
32 |
Correct |
873 ms |
82116 KB |
Output is correct |
33 |
Correct |
914 ms |
82256 KB |
Output is correct |
34 |
Correct |
867 ms |
79880 KB |
Output is correct |
35 |
Correct |
842 ms |
79500 KB |
Output is correct |
36 |
Correct |
924 ms |
79504 KB |
Output is correct |
37 |
Correct |
870 ms |
81032 KB |
Output is correct |
38 |
Correct |
625 ms |
87864 KB |
Output is correct |
39 |
Correct |
622 ms |
87808 KB |
Output is correct |
40 |
Correct |
618 ms |
84420 KB |
Output is correct |
41 |
Correct |
619 ms |
84092 KB |
Output is correct |
42 |
Correct |
612 ms |
84120 KB |
Output is correct |
43 |
Correct |
619 ms |
85796 KB |
Output is correct |
44 |
Correct |
669 ms |
87168 KB |
Output is correct |
45 |
Correct |
647 ms |
87256 KB |
Output is correct |
46 |
Correct |
639 ms |
84144 KB |
Output is correct |
47 |
Correct |
644 ms |
83692 KB |
Output is correct |
48 |
Correct |
641 ms |
83548 KB |
Output is correct |
49 |
Correct |
666 ms |
85716 KB |
Output is correct |
50 |
Correct |
723 ms |
85268 KB |
Output is correct |
51 |
Correct |
712 ms |
85096 KB |
Output is correct |
52 |
Correct |
694 ms |
82684 KB |
Output is correct |
53 |
Correct |
703 ms |
82444 KB |
Output is correct |
54 |
Correct |
694 ms |
82276 KB |
Output is correct |
55 |
Correct |
726 ms |
83908 KB |
Output is correct |