#include <bits/stdc++.h>
using namespace std;
typedef array<int, 3> Node; // 0/1/2: c/ab/abc
const int maxn = 1 << 19;
const int INF = 1e9;
int n, q;
int A[maxn];
vector<int> p[maxn];
vector<pair<int, int>> query[maxn];
struct segtree {
Node T[maxn << 1];
Node upd(const Node&x, const Node& y) {
return {max(x[0], y[0]), max(x[1], y[1]), max({x[1] + y[0], x[2], y[2]})};
}
void init() {
for (int i = maxn; i < maxn << 1; ++i) T[i] = {A[i - maxn], -INF, -INF};
for (int i = maxn - 1; i > 0; --i) T[i] = upd(T[2 * i], T[2 * i + 1]);
}
void update(int i, int v) {
i += maxn;
T[i][1] = max(T[i][1], v);
T[i][2] = max(T[i][2], T[i][1] + T[i][0]);
for (i >>= 1; i > 0; i >>= 1) {
T[i] = upd(T[2 * i], T[2 * i + 1]);
}
}
int get(int l, int r) {
Node L = {-INF, -INF, -INF};
Node R = L;
for (l += maxn, r += maxn; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
L = upd(L, T[l]);
l++;
}
if (r & 1) {
r--;
R = upd(T[r], R);
}
}
return upd(L, R)[2];
}
} seg;
#define task "code"
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
// freopen(task".inp", "r", stdin);
// freopen(task".ans", "w", stdout);
cin >> n;
stack<int> st;
for (int i = 0; i < n; ++i) {
cin >> A[i];
while (!st.empty()) {
int j = st.top(), k = 2 * i - j;
if (k < n) p[j].push_back(k);
if (A[j] <= A[i]) st.pop();
else break;
}
st.push(i);
}
seg.init();
cin >> q;
vector<int> answer(q, 0);
for (int i = 0; i < q; ++i) {
int l, r; cin >> l >> r;
query[l - 1].emplace_back(r - 1, i);
}
for (int i = n - 1; i >= 0; --i) {
for (int j : p[i]) {
seg.update(j, A[i] + A[(i + j) / 2]); }
for (auto it : query[i]) {
answer[it.second] = seg.get(i, it.first + 1);
}
}
for (int i = 0; i < q; ++i) cout << answer[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
37208 KB |
Output is correct |
2 |
Correct |
12 ms |
37212 KB |
Output is correct |
3 |
Correct |
13 ms |
37212 KB |
Output is correct |
4 |
Correct |
13 ms |
37340 KB |
Output is correct |
5 |
Correct |
13 ms |
37388 KB |
Output is correct |
6 |
Correct |
12 ms |
37164 KB |
Output is correct |
7 |
Correct |
12 ms |
37364 KB |
Output is correct |
8 |
Correct |
12 ms |
37208 KB |
Output is correct |
9 |
Correct |
12 ms |
37212 KB |
Output is correct |
10 |
Correct |
12 ms |
37212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
37208 KB |
Output is correct |
2 |
Correct |
12 ms |
37212 KB |
Output is correct |
3 |
Correct |
13 ms |
37212 KB |
Output is correct |
4 |
Correct |
13 ms |
37340 KB |
Output is correct |
5 |
Correct |
13 ms |
37388 KB |
Output is correct |
6 |
Correct |
12 ms |
37164 KB |
Output is correct |
7 |
Correct |
12 ms |
37364 KB |
Output is correct |
8 |
Correct |
12 ms |
37208 KB |
Output is correct |
9 |
Correct |
12 ms |
37212 KB |
Output is correct |
10 |
Correct |
12 ms |
37212 KB |
Output is correct |
11 |
Correct |
125 ms |
56020 KB |
Output is correct |
12 |
Correct |
124 ms |
56148 KB |
Output is correct |
13 |
Correct |
119 ms |
56144 KB |
Output is correct |
14 |
Correct |
126 ms |
56144 KB |
Output is correct |
15 |
Correct |
125 ms |
56184 KB |
Output is correct |
16 |
Correct |
123 ms |
55380 KB |
Output is correct |
17 |
Correct |
148 ms |
55316 KB |
Output is correct |
18 |
Correct |
134 ms |
55376 KB |
Output is correct |
19 |
Correct |
132 ms |
56044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
46416 KB |
Output is correct |
2 |
Correct |
49 ms |
45964 KB |
Output is correct |
3 |
Correct |
49 ms |
46932 KB |
Output is correct |
4 |
Correct |
74 ms |
46344 KB |
Output is correct |
5 |
Correct |
79 ms |
46164 KB |
Output is correct |
6 |
Correct |
70 ms |
45648 KB |
Output is correct |
7 |
Correct |
72 ms |
45520 KB |
Output is correct |
8 |
Correct |
69 ms |
45404 KB |
Output is correct |
9 |
Correct |
70 ms |
45904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
37208 KB |
Output is correct |
2 |
Correct |
12 ms |
37212 KB |
Output is correct |
3 |
Correct |
13 ms |
37212 KB |
Output is correct |
4 |
Correct |
13 ms |
37340 KB |
Output is correct |
5 |
Correct |
13 ms |
37388 KB |
Output is correct |
6 |
Correct |
12 ms |
37164 KB |
Output is correct |
7 |
Correct |
12 ms |
37364 KB |
Output is correct |
8 |
Correct |
12 ms |
37208 KB |
Output is correct |
9 |
Correct |
12 ms |
37212 KB |
Output is correct |
10 |
Correct |
12 ms |
37212 KB |
Output is correct |
11 |
Correct |
125 ms |
56020 KB |
Output is correct |
12 |
Correct |
124 ms |
56148 KB |
Output is correct |
13 |
Correct |
119 ms |
56144 KB |
Output is correct |
14 |
Correct |
126 ms |
56144 KB |
Output is correct |
15 |
Correct |
125 ms |
56184 KB |
Output is correct |
16 |
Correct |
123 ms |
55380 KB |
Output is correct |
17 |
Correct |
148 ms |
55316 KB |
Output is correct |
18 |
Correct |
134 ms |
55376 KB |
Output is correct |
19 |
Correct |
132 ms |
56044 KB |
Output is correct |
20 |
Correct |
75 ms |
46416 KB |
Output is correct |
21 |
Correct |
49 ms |
45964 KB |
Output is correct |
22 |
Correct |
49 ms |
46932 KB |
Output is correct |
23 |
Correct |
74 ms |
46344 KB |
Output is correct |
24 |
Correct |
79 ms |
46164 KB |
Output is correct |
25 |
Correct |
70 ms |
45648 KB |
Output is correct |
26 |
Correct |
72 ms |
45520 KB |
Output is correct |
27 |
Correct |
69 ms |
45404 KB |
Output is correct |
28 |
Correct |
70 ms |
45904 KB |
Output is correct |
29 |
Correct |
424 ms |
83396 KB |
Output is correct |
30 |
Correct |
316 ms |
82724 KB |
Output is correct |
31 |
Correct |
327 ms |
84820 KB |
Output is correct |
32 |
Correct |
424 ms |
83308 KB |
Output is correct |
33 |
Correct |
381 ms |
83280 KB |
Output is correct |
34 |
Correct |
378 ms |
80980 KB |
Output is correct |
35 |
Correct |
388 ms |
80736 KB |
Output is correct |
36 |
Correct |
384 ms |
80724 KB |
Output is correct |
37 |
Correct |
373 ms |
82092 KB |
Output is correct |
38 |
Correct |
285 ms |
88948 KB |
Output is correct |
39 |
Correct |
262 ms |
88920 KB |
Output is correct |
40 |
Correct |
267 ms |
85764 KB |
Output is correct |
41 |
Correct |
277 ms |
85216 KB |
Output is correct |
42 |
Correct |
290 ms |
85264 KB |
Output is correct |
43 |
Correct |
269 ms |
86912 KB |
Output is correct |
44 |
Correct |
322 ms |
88552 KB |
Output is correct |
45 |
Correct |
298 ms |
88656 KB |
Output is correct |
46 |
Correct |
298 ms |
85332 KB |
Output is correct |
47 |
Correct |
285 ms |
84772 KB |
Output is correct |
48 |
Correct |
275 ms |
84900 KB |
Output is correct |
49 |
Correct |
274 ms |
86868 KB |
Output is correct |
50 |
Correct |
318 ms |
86612 KB |
Output is correct |
51 |
Correct |
311 ms |
86460 KB |
Output is correct |
52 |
Correct |
317 ms |
83984 KB |
Output is correct |
53 |
Correct |
312 ms |
83748 KB |
Output is correct |
54 |
Correct |
320 ms |
83796 KB |
Output is correct |
55 |
Correct |
342 ms |
85412 KB |
Output is correct |