#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int q;
cin >> q;
vector<vector<pair<int, int>>> qry(n);
vector<vector<int>> g(n);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
l--, r--;
qry[l].emplace_back(r, i);
}
vector<int> st;
for (int i = 0; i < n; ++i) {
while (st.size() && a[st.back()] < a[i]) {
st.pop_back();
}
if (st.size()) {
g[st.back()].emplace_back(i);
}
st.emplace_back(i);
}
while (st.size()) {
st.pop_back();
}
for (int i = n - 1; ~i; --i) {
while (st.size() && a[st.back()] <= a[i]) {
st.pop_back();
}
if (st.size()) {
g[i].emplace_back(st.back());
}
st.emplace_back(i);
}
const int INF = int(1e9);
vector<int> s(4 << __lg(n), -INF);
auto lz = s;
auto mx = s;
auto app = [&](int id, int x) {
lz[id] = max(lz[id], x);
s[id] = max(s[id], x + mx[id]);
};
auto psh = [&](int id) {
app(id * 2, lz[id]);
app(id * 2 + 1, lz[id]);
lz[id] = -INF;
};
auto updP = [&](int i) {
int id = 1, l = 0, r = n - 1;
while (l ^ r) {
int md = (l + r) / 2;
if (lz[id] != -INF) {
psh(id);
}
id *= 2;
if (i <= md) {
r = md;
} else {
l = md + 1;
id += 1;
}
}
mx[id] = a[i];
while (id > 1) {
id /= 2;
mx[id] = max(mx[id * 2], mx[id * 2 + 1]);
}
};
auto upd = [&](int i, int x) {
int id = 1, l = 0, r = n - 1;
while (l ^ r) {
int md = (l + r) / 2;
if (lz[id] != -INF) {
psh(id);
}
id *= 2;
if (i - 1 <= md) {
app(id + 1, x);
r = md;
} else {
id += 1;
l = md + 1;
}
}
while (id > 1) {
id /= 2;
s[id] = max(s[id * 2], s[id * 2 + 1]);
}
};
auto get = [&](int i) {
int id = 1, l = 0, r = n - 1;
int res = -INF;
while (l ^ r) {
int md = (l + r) / 2;
if (lz[id] != -INF) {
psh(id);
}
id *= 2;
if (i <= md) {
r = md;
} else {
res = max(res, s[id]);
l = md + 1;
id += 1;
}
}
return max(res, s[id]);
};
vector<int> res(q);
for (int l = n - 1; ~l; --l) {
updP(l);
for (int r : g[l]) {
upd(2 * r - l, a[l] + a[r]);
}
for (auto [r, id] : qry[l]) {
res[id] = get(r);
}
}
for (int i = 0; i < q; ++i) {
cout << res[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
141 ms |
19396 KB |
Output is correct |
12 |
Correct |
148 ms |
19468 KB |
Output is correct |
13 |
Correct |
140 ms |
19544 KB |
Output is correct |
14 |
Correct |
139 ms |
19372 KB |
Output is correct |
15 |
Correct |
139 ms |
19560 KB |
Output is correct |
16 |
Correct |
143 ms |
18772 KB |
Output is correct |
17 |
Correct |
152 ms |
18772 KB |
Output is correct |
18 |
Correct |
144 ms |
18768 KB |
Output is correct |
19 |
Correct |
141 ms |
19284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
24924 KB |
Output is correct |
2 |
Correct |
71 ms |
25584 KB |
Output is correct |
3 |
Correct |
70 ms |
25540 KB |
Output is correct |
4 |
Correct |
107 ms |
25024 KB |
Output is correct |
5 |
Correct |
107 ms |
24968 KB |
Output is correct |
6 |
Correct |
103 ms |
24144 KB |
Output is correct |
7 |
Correct |
102 ms |
24144 KB |
Output is correct |
8 |
Correct |
102 ms |
24084 KB |
Output is correct |
9 |
Correct |
103 ms |
24400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
141 ms |
19396 KB |
Output is correct |
12 |
Correct |
148 ms |
19468 KB |
Output is correct |
13 |
Correct |
140 ms |
19544 KB |
Output is correct |
14 |
Correct |
139 ms |
19372 KB |
Output is correct |
15 |
Correct |
139 ms |
19560 KB |
Output is correct |
16 |
Correct |
143 ms |
18772 KB |
Output is correct |
17 |
Correct |
152 ms |
18772 KB |
Output is correct |
18 |
Correct |
144 ms |
18768 KB |
Output is correct |
19 |
Correct |
141 ms |
19284 KB |
Output is correct |
20 |
Correct |
106 ms |
24924 KB |
Output is correct |
21 |
Correct |
71 ms |
25584 KB |
Output is correct |
22 |
Correct |
70 ms |
25540 KB |
Output is correct |
23 |
Correct |
107 ms |
25024 KB |
Output is correct |
24 |
Correct |
107 ms |
24968 KB |
Output is correct |
25 |
Correct |
103 ms |
24144 KB |
Output is correct |
26 |
Correct |
102 ms |
24144 KB |
Output is correct |
27 |
Correct |
102 ms |
24084 KB |
Output is correct |
28 |
Correct |
103 ms |
24400 KB |
Output is correct |
29 |
Correct |
601 ms |
82148 KB |
Output is correct |
30 |
Correct |
495 ms |
83652 KB |
Output is correct |
31 |
Correct |
499 ms |
83568 KB |
Output is correct |
32 |
Correct |
617 ms |
82376 KB |
Output is correct |
33 |
Correct |
624 ms |
82236 KB |
Output is correct |
34 |
Correct |
614 ms |
80188 KB |
Output is correct |
35 |
Correct |
632 ms |
79976 KB |
Output is correct |
36 |
Correct |
602 ms |
79700 KB |
Output is correct |
37 |
Correct |
637 ms |
81236 KB |
Output is correct |
38 |
Correct |
478 ms |
87812 KB |
Output is correct |
39 |
Correct |
439 ms |
87896 KB |
Output is correct |
40 |
Correct |
433 ms |
84820 KB |
Output is correct |
41 |
Correct |
434 ms |
84136 KB |
Output is correct |
42 |
Correct |
426 ms |
84136 KB |
Output is correct |
43 |
Correct |
441 ms |
86192 KB |
Output is correct |
44 |
Correct |
484 ms |
87540 KB |
Output is correct |
45 |
Correct |
471 ms |
87280 KB |
Output is correct |
46 |
Correct |
454 ms |
84056 KB |
Output is correct |
47 |
Correct |
449 ms |
83752 KB |
Output is correct |
48 |
Correct |
449 ms |
83848 KB |
Output is correct |
49 |
Correct |
501 ms |
85860 KB |
Output is correct |
50 |
Correct |
500 ms |
85044 KB |
Output is correct |
51 |
Correct |
498 ms |
85160 KB |
Output is correct |
52 |
Correct |
489 ms |
82708 KB |
Output is correct |
53 |
Correct |
514 ms |
82656 KB |
Output is correct |
54 |
Correct |
491 ms |
82656 KB |
Output is correct |
55 |
Correct |
518 ms |
83928 KB |
Output is correct |