#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5, inf = 1e9;
vector<pair<int, int>> cands[maxn], queries[maxn];
struct SegTree {
int n, N;
vector<int> bestP, sum, lazy;
SegTree() {}
SegTree(int _n) {
n = _n, N = 1;
while (N < n) {
N <<= 1;
}
bestP.resize(2 * N, -inf);
sum.resize(2 * N, -inf);
lazy.resize(2 * N, -inf);
}
void push(int v) {
sum[v] = max(sum[v], bestP[v] + lazy[v]);
if (v < N) {
lazy[2 * v] = max(lazy[2 * v], lazy[v]);
lazy[2 * v + 1] = max(lazy[2 * v + 1], lazy[v]);
}
lazy[v] = -inf;
}
void insertP(int v, int tl, int tr, int i, int x) {
if (lazy[v] != -inf) {
push(v);
}
if (tl > i || tr <= i) {
return;
}
if (tl + 1 == tr) {
bestP[v] = max(bestP[v], x);
return;
}
int m = tl + (tr - tl) / 2;
insertP(2 * v, tl, m, i, x);
insertP(2 * v + 1, m, tr, i, x);
bestP[v] = max(bestP[2 * v], bestP[2 * v + 1]);
}
void insertP(int i, int x) {
insertP(1, 0, N, i, x);
}
void maxEq(int v, int tl, int tr, int l, int r, int x) {
if (lazy[v] != -inf) {
push(v);
}
if (tl >= r || tr <= l) {
return;
}
if (l <= tl && tr <= r) {
lazy[v] = x;
push(v);
return;
}
int m = tl + (tr - tl) / 2;
maxEq(2 * v, tl, m, l, r, x);
maxEq(2 * v + 1, m, tr, l, r, x);
sum[v] = max(sum[2 * v], sum[2 * v + 1]);
}
void maxEq(int l, int r, int x) {
maxEq(1, 0, N, l, r, x);
}
int getMax(int v, int tl, int tr, int l, int r) {
if (lazy[v] != -inf) {
push(v);
}
if (tl >= r || tr <= l) {
return -inf;
}
if (l <= tl && tr <= r) {
return sum[v];
}
int m = tl + (tr - tl) / 2;
return max(getMax(2 * v, tl, m, l, r), getMax(2 * v + 1, m, tr, l, r));
}
int getMax(int l, int r) {
return getMax(1, 0, N, l, r);
}
~SegTree() {}
};
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> leftGreq(n, -1), rightGreq(n, n), stk;
for (int i = 0; i < n; ++i) {
while (!stk.empty() && a[stk.back()] < a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
leftGreq[i] = stk.back();
}
stk.emplace_back(i);
}
stk.clear();
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty() && a[stk.back()] < a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
rightGreq[i] = stk.back();
}
stk.emplace_back(i);
}
stk.clear();
for (int i = 0; i < n; ++i) {
if (rightGreq[i] != n && rightGreq[i] - i + rightGreq[i] < n) {
cands[2 * rightGreq[i] - i].emplace_back(i, rightGreq[i]);
}
}
for (int j = n - 1; j >= 0; --j) {
if (leftGreq[j] != -1 && j - leftGreq[j] + j < n) {
cands[2 * j - leftGreq[j]].emplace_back(leftGreq[j], j);
}
}
int q;
cin >> q;
vector<int> ans(q);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
queries[r].emplace_back(l, i);
}
SegTree st(n);
for (int r = 0; r < n; ++r) {
while (!cands[r].empty()) {
auto [i, j] = cands[r].back();
cands[r].pop_back();
st.insertP(i, a[i] + a[j]);
}
st.maxEq(0, r, a[r]);
while (!queries[r].empty()) {
auto [l, i] = queries[r].back();
queries[r].pop_back();
ans[i] = st.getMax(l, r);
}
}
for (const int& x : ans) {
cout << x << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
cin >> T;
while (T--) {
solve();
cerr << "-----------\n";
cout << "-----------\n";
}
#else
int T = 1;
// cin >> T;
while (T--) {
solve();
}
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23900 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
5 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
6 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23900 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
5 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
6 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
214 ms |
38904 KB |
Output is correct |
12 |
Correct |
224 ms |
42836 KB |
Output is correct |
13 |
Correct |
220 ms |
42860 KB |
Output is correct |
14 |
Correct |
221 ms |
43048 KB |
Output is correct |
15 |
Correct |
221 ms |
42832 KB |
Output is correct |
16 |
Correct |
218 ms |
42296 KB |
Output is correct |
17 |
Correct |
210 ms |
42256 KB |
Output is correct |
18 |
Correct |
226 ms |
42000 KB |
Output is correct |
19 |
Correct |
208 ms |
42832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
39504 KB |
Output is correct |
2 |
Correct |
97 ms |
39508 KB |
Output is correct |
3 |
Correct |
107 ms |
39504 KB |
Output is correct |
4 |
Correct |
146 ms |
39248 KB |
Output is correct |
5 |
Correct |
147 ms |
39492 KB |
Output is correct |
6 |
Correct |
147 ms |
39488 KB |
Output is correct |
7 |
Correct |
151 ms |
39496 KB |
Output is correct |
8 |
Correct |
153 ms |
39488 KB |
Output is correct |
9 |
Correct |
144 ms |
39748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23900 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
5 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
6 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
214 ms |
38904 KB |
Output is correct |
12 |
Correct |
224 ms |
42836 KB |
Output is correct |
13 |
Correct |
220 ms |
42860 KB |
Output is correct |
14 |
Correct |
221 ms |
43048 KB |
Output is correct |
15 |
Correct |
221 ms |
42832 KB |
Output is correct |
16 |
Correct |
218 ms |
42296 KB |
Output is correct |
17 |
Correct |
210 ms |
42256 KB |
Output is correct |
18 |
Correct |
226 ms |
42000 KB |
Output is correct |
19 |
Correct |
208 ms |
42832 KB |
Output is correct |
20 |
Correct |
166 ms |
39504 KB |
Output is correct |
21 |
Correct |
97 ms |
39508 KB |
Output is correct |
22 |
Correct |
107 ms |
39504 KB |
Output is correct |
23 |
Correct |
146 ms |
39248 KB |
Output is correct |
24 |
Correct |
147 ms |
39492 KB |
Output is correct |
25 |
Correct |
147 ms |
39488 KB |
Output is correct |
26 |
Correct |
151 ms |
39496 KB |
Output is correct |
27 |
Correct |
153 ms |
39488 KB |
Output is correct |
28 |
Correct |
144 ms |
39748 KB |
Output is correct |
29 |
Correct |
826 ms |
87444 KB |
Output is correct |
30 |
Correct |
725 ms |
87492 KB |
Output is correct |
31 |
Correct |
706 ms |
87484 KB |
Output is correct |
32 |
Correct |
839 ms |
87584 KB |
Output is correct |
33 |
Correct |
826 ms |
87444 KB |
Output is correct |
34 |
Correct |
860 ms |
85292 KB |
Output is correct |
35 |
Correct |
859 ms |
84804 KB |
Output is correct |
36 |
Correct |
810 ms |
84896 KB |
Output is correct |
37 |
Correct |
852 ms |
86396 KB |
Output is correct |
38 |
Correct |
647 ms |
93104 KB |
Output is correct |
39 |
Correct |
659 ms |
93260 KB |
Output is correct |
40 |
Correct |
690 ms |
90196 KB |
Output is correct |
41 |
Correct |
637 ms |
89524 KB |
Output is correct |
42 |
Correct |
646 ms |
89620 KB |
Output is correct |
43 |
Correct |
627 ms |
91160 KB |
Output is correct |
44 |
Correct |
678 ms |
92496 KB |
Output is correct |
45 |
Correct |
662 ms |
92536 KB |
Output is correct |
46 |
Correct |
710 ms |
89424 KB |
Output is correct |
47 |
Correct |
642 ms |
89084 KB |
Output is correct |
48 |
Correct |
685 ms |
89080 KB |
Output is correct |
49 |
Correct |
678 ms |
91128 KB |
Output is correct |
50 |
Correct |
740 ms |
90788 KB |
Output is correct |
51 |
Correct |
738 ms |
90884 KB |
Output is correct |
52 |
Correct |
710 ms |
88240 KB |
Output is correct |
53 |
Correct |
707 ms |
87908 KB |
Output is correct |
54 |
Correct |
740 ms |
87980 KB |
Output is correct |
55 |
Correct |
730 ms |
89596 KB |
Output is correct |