/**
* author: NimaAryan
* created: 2024-01-16 23:23:10
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
struct Tag {
int add = 0;
void apply(const Tag& t) {
add = max(add, t.add);
}
};
struct Info {
int val = 0;
int best = 0;
void apply(const Tag& t) {
best = max(best, val + t.add);
}
};
Info operator+(const Info& a, const Info& b) {
return {max(a.val, b.val), max(a.best, b.best)};
}
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree(int n) : n(n) {
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag& v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
}
void modify(int p, int l, int r, int x, const Info& v) {
if (r - l == 1) {
info[p] = v;
return;
}
push(p);
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int x, const Info& v) {
modify(1, 0, n, x, v);
}
void range_apply(int p, int l, int r, int lx, int rx, const Tag& v) {
if (l >= rx || r <= lx) {
return;
}
if (l >= lx && r <= rx) {
apply(p, v);
return;
}
push(p);
int m = (l + r) / 2;
range_apply(2 * p, l, m, lx, rx, v);
range_apply(2 * p + 1, m, r, lx, rx, v);
pull(p);
}
void range_apply(int lx, int rx, const Tag& v) {
range_apply(1, 0, n, lx, rx, v);
}
Info range_query(int p, int l, int r, int lx, int rx) {
if (l >= rx || r <= lx) {
return Info();
}
if (l >= lx && r <= rx) {
return info[p];
}
push(p);
int m = (l + r) / 2;
return range_query(2 * p, l, m, lx, rx) +
range_query(2 * p + 1, m, r, lx, rx);
}
Info range_query(int lx, int rx) {
return range_query(1, 0, n, lx, rx);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int q;
cin >> q;
vector<int> l(q), r(q);
vector<vector<int>> qs(n);
for (int i = 0; i < q; ++i) {
cin >> l[i] >> r[i];
--l[i];
qs[l[i]].push_back(i);
}
vector<int> ans(q);
LazySegmentTree seg(n);
for (int i = 0; i < n; ++i) {
seg.modify(i, {a[i], 0});
}
vector<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty()) {
int j = stk.back();
stk.pop_back();
seg.range_apply(j + (j - i), n, Tag{a[i] + a[j]});
if (a[j] > a[i]) {
stk.push_back(j);
break;
}
}
stk.push_back(i);
for (int p : qs[i]) {
ans[p] = seg.range_query(i, r[p]).best;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
212 ms |
15040 KB |
Output is correct |
12 |
Correct |
217 ms |
15188 KB |
Output is correct |
13 |
Correct |
209 ms |
14928 KB |
Output is correct |
14 |
Correct |
203 ms |
15184 KB |
Output is correct |
15 |
Correct |
233 ms |
15092 KB |
Output is correct |
16 |
Correct |
207 ms |
14416 KB |
Output is correct |
17 |
Correct |
217 ms |
14300 KB |
Output is correct |
18 |
Correct |
207 ms |
14448 KB |
Output is correct |
19 |
Correct |
223 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
11868 KB |
Output is correct |
2 |
Correct |
96 ms |
13096 KB |
Output is correct |
3 |
Correct |
98 ms |
12116 KB |
Output is correct |
4 |
Correct |
146 ms |
12088 KB |
Output is correct |
5 |
Correct |
138 ms |
11952 KB |
Output is correct |
6 |
Correct |
134 ms |
11864 KB |
Output is correct |
7 |
Correct |
163 ms |
12092 KB |
Output is correct |
8 |
Correct |
138 ms |
12088 KB |
Output is correct |
9 |
Correct |
136 ms |
12088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
212 ms |
15040 KB |
Output is correct |
12 |
Correct |
217 ms |
15188 KB |
Output is correct |
13 |
Correct |
209 ms |
14928 KB |
Output is correct |
14 |
Correct |
203 ms |
15184 KB |
Output is correct |
15 |
Correct |
233 ms |
15092 KB |
Output is correct |
16 |
Correct |
207 ms |
14416 KB |
Output is correct |
17 |
Correct |
217 ms |
14300 KB |
Output is correct |
18 |
Correct |
207 ms |
14448 KB |
Output is correct |
19 |
Correct |
223 ms |
14928 KB |
Output is correct |
20 |
Correct |
158 ms |
11868 KB |
Output is correct |
21 |
Correct |
96 ms |
13096 KB |
Output is correct |
22 |
Correct |
98 ms |
12116 KB |
Output is correct |
23 |
Correct |
146 ms |
12088 KB |
Output is correct |
24 |
Correct |
138 ms |
11952 KB |
Output is correct |
25 |
Correct |
134 ms |
11864 KB |
Output is correct |
26 |
Correct |
163 ms |
12092 KB |
Output is correct |
27 |
Correct |
138 ms |
12088 KB |
Output is correct |
28 |
Correct |
136 ms |
12088 KB |
Output is correct |
29 |
Correct |
877 ms |
46164 KB |
Output is correct |
30 |
Correct |
810 ms |
48152 KB |
Output is correct |
31 |
Correct |
778 ms |
46440 KB |
Output is correct |
32 |
Correct |
848 ms |
46180 KB |
Output is correct |
33 |
Correct |
887 ms |
46308 KB |
Output is correct |
34 |
Correct |
878 ms |
45676 KB |
Output is correct |
35 |
Correct |
839 ms |
45512 KB |
Output is correct |
36 |
Correct |
930 ms |
45424 KB |
Output is correct |
37 |
Correct |
901 ms |
46188 KB |
Output is correct |
38 |
Correct |
609 ms |
52800 KB |
Output is correct |
39 |
Correct |
609 ms |
52864 KB |
Output is correct |
40 |
Correct |
630 ms |
51260 KB |
Output is correct |
41 |
Correct |
597 ms |
51024 KB |
Output is correct |
42 |
Correct |
606 ms |
51012 KB |
Output is correct |
43 |
Correct |
609 ms |
51940 KB |
Output is correct |
44 |
Correct |
637 ms |
52132 KB |
Output is correct |
45 |
Correct |
640 ms |
52156 KB |
Output is correct |
46 |
Correct |
630 ms |
50648 KB |
Output is correct |
47 |
Correct |
644 ms |
50712 KB |
Output is correct |
48 |
Correct |
655 ms |
50764 KB |
Output is correct |
49 |
Correct |
643 ms |
51712 KB |
Output is correct |
50 |
Correct |
727 ms |
49856 KB |
Output is correct |
51 |
Correct |
763 ms |
49868 KB |
Output is correct |
52 |
Correct |
729 ms |
49084 KB |
Output is correct |
53 |
Correct |
723 ms |
49232 KB |
Output is correct |
54 |
Correct |
753 ms |
49008 KB |
Output is correct |
55 |
Correct |
781 ms |
49860 KB |
Output is correct |