#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define ff first
#define ss second
const int N = 5e5 + 5;
int n, q;
int a[N];
struct Query {
int l, r, id;
bool operator < (const Query &o) const { return l > o.l; }
} Q[N];
int st[N], top;
int cnt;
pii p[N << 1];
void init() {
for (int i = 1; i <= n; ++i) {
while (top && a[st[top]] < a[i]) --top;
if (top) p[++cnt] = pii(st[top], i);
st[++top] = i;
}
top = 0;
for (int i = n; i; --i) {
while (top && a[st[top]] < a[i]) --top;
if (top) p[++cnt] = pii(i, st[top]);
st[++top] = i;
}
}
int seg[1 << 20], laz[1 << 20], ma1[1 << 20], ma2[1 << 20];
void build(int u, int l, int r) {
if (l == r) seg[u] = ma1[u] = a[l];
else {
int m = (l + r) >> 1, u1 = u << 1, u2 = u1 | 1;
build(u1, l, m);
build(u2, m + 1, r);
seg[u] = ma1[u] = max(ma1[u1], ma1[u2]);
}
}
void update(int u, int l, int r, int a, int b, int v) {
if (a <= l && r <= b) {
if (ma2[u] < v) {
ma2[u] = laz[u] = v;
seg[u] = max(seg[u], v + ma1[u]);
}
return;
}
int m = (l + r) >> 1, u1 = u << 1, u2 = u1 | 1;
if (laz[u]) {
if (laz[u] > ma2[u1]) {
ma2[u1] = laz[u1] = laz[u];
seg[u1] = max(seg[u1], laz[u] + ma1[u1]);
}
if (laz[u] > ma2[u2]) {
ma2[u2] = laz[u2] = laz[u];
seg[u2] = max(seg[u2], laz[u] + ma1[u2]);
}
laz[u] = 0;
}
if (a <= m) update(u1, l, m, a, b, v);
if (m < b) update(u2, m + 1, r, a, b, v);
seg[u] = max(seg[u1], seg[u2]);
}
void update(int l, int r, int v) {
if (l <= r) update(1, 1, n, l, r, v);
}
int get(int u, int l, int r, int a, int b) {
if (a <= l && r <= b) return seg[u];
if (b < l || r < a) return 0;
int m = (l + r) >> 1, u1 = u << 1, u2 = u1 | 1;
if (laz[u]) {
if (laz[u] > ma2[u1]) {
ma2[u1] = laz[u1] = laz[u];
seg[u1] = max(seg[u1], laz[u] + ma1[u1]);
}
if (laz[u] > ma2[u2]) {
ma2[u2] = laz[u2] = laz[u];
seg[u2] = max(seg[u2], laz[u] + ma1[u2]);
}
laz[u] = 0;
}
return max(get(u1, l, m, a, b), get(u2, m + 1, r, a, b));
}
int ans[N];
void solve() {
init();
sort(Q + 1, Q + q + 1);
sort(p + 1, p + cnt + 1, [](const pii &a, const pii &b) { return a.ff > b.ff; });
build(1, 1, n);
for (int i = 1, j = 1; i <= q; ++i) {
while (j <= cnt && p[j].ff >= Q[i].l) {
update(p[j].ss * 2 - p[j].ff, n, a[p[j].ff] + a[p[j].ss]);
++j;
}
ans[Q[i].id] = get(1, 1, n, Q[i].l, Q[i].r);
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cin >> q;
for (int i = 1; i <= q; ++i)
cin >> Q[i].l >> Q[i].r, Q[i].id = i;
solve();
for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
388 KB |
Output is correct |
11 |
Correct |
233 ms |
18216 KB |
Output is correct |
12 |
Correct |
233 ms |
18136 KB |
Output is correct |
13 |
Correct |
234 ms |
18180 KB |
Output is correct |
14 |
Correct |
275 ms |
18216 KB |
Output is correct |
15 |
Correct |
239 ms |
18172 KB |
Output is correct |
16 |
Correct |
225 ms |
17592 KB |
Output is correct |
17 |
Correct |
254 ms |
17480 KB |
Output is correct |
18 |
Correct |
252 ms |
17452 KB |
Output is correct |
19 |
Correct |
224 ms |
18044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
12376 KB |
Output is correct |
2 |
Correct |
83 ms |
13448 KB |
Output is correct |
3 |
Correct |
75 ms |
13428 KB |
Output is correct |
4 |
Correct |
140 ms |
14148 KB |
Output is correct |
5 |
Correct |
162 ms |
14316 KB |
Output is correct |
6 |
Correct |
134 ms |
13584 KB |
Output is correct |
7 |
Correct |
140 ms |
13460 KB |
Output is correct |
8 |
Correct |
138 ms |
13376 KB |
Output is correct |
9 |
Correct |
136 ms |
13760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
388 KB |
Output is correct |
11 |
Correct |
233 ms |
18216 KB |
Output is correct |
12 |
Correct |
233 ms |
18136 KB |
Output is correct |
13 |
Correct |
234 ms |
18180 KB |
Output is correct |
14 |
Correct |
275 ms |
18216 KB |
Output is correct |
15 |
Correct |
239 ms |
18172 KB |
Output is correct |
16 |
Correct |
225 ms |
17592 KB |
Output is correct |
17 |
Correct |
254 ms |
17480 KB |
Output is correct |
18 |
Correct |
252 ms |
17452 KB |
Output is correct |
19 |
Correct |
224 ms |
18044 KB |
Output is correct |
20 |
Correct |
177 ms |
12376 KB |
Output is correct |
21 |
Correct |
83 ms |
13448 KB |
Output is correct |
22 |
Correct |
75 ms |
13428 KB |
Output is correct |
23 |
Correct |
140 ms |
14148 KB |
Output is correct |
24 |
Correct |
162 ms |
14316 KB |
Output is correct |
25 |
Correct |
134 ms |
13584 KB |
Output is correct |
26 |
Correct |
140 ms |
13460 KB |
Output is correct |
27 |
Correct |
138 ms |
13376 KB |
Output is correct |
28 |
Correct |
136 ms |
13760 KB |
Output is correct |
29 |
Correct |
830 ms |
50248 KB |
Output is correct |
30 |
Correct |
601 ms |
48448 KB |
Output is correct |
31 |
Correct |
671 ms |
48324 KB |
Output is correct |
32 |
Correct |
781 ms |
50308 KB |
Output is correct |
33 |
Correct |
818 ms |
50344 KB |
Output is correct |
34 |
Correct |
750 ms |
48108 KB |
Output is correct |
35 |
Correct |
748 ms |
47796 KB |
Output is correct |
36 |
Correct |
812 ms |
47752 KB |
Output is correct |
37 |
Correct |
776 ms |
49216 KB |
Output is correct |
38 |
Correct |
641 ms |
50424 KB |
Output is correct |
39 |
Correct |
631 ms |
50264 KB |
Output is correct |
40 |
Correct |
580 ms |
47052 KB |
Output is correct |
41 |
Correct |
588 ms |
46596 KB |
Output is correct |
42 |
Correct |
621 ms |
46492 KB |
Output is correct |
43 |
Correct |
616 ms |
48256 KB |
Output is correct |
44 |
Correct |
604 ms |
50508 KB |
Output is correct |
45 |
Correct |
620 ms |
50272 KB |
Output is correct |
46 |
Correct |
593 ms |
47168 KB |
Output is correct |
47 |
Correct |
610 ms |
46732 KB |
Output is correct |
48 |
Correct |
614 ms |
46840 KB |
Output is correct |
49 |
Correct |
655 ms |
48740 KB |
Output is correct |
50 |
Correct |
679 ms |
50304 KB |
Output is correct |
51 |
Correct |
650 ms |
50388 KB |
Output is correct |
52 |
Correct |
715 ms |
47908 KB |
Output is correct |
53 |
Correct |
678 ms |
47588 KB |
Output is correct |
54 |
Correct |
659 ms |
47608 KB |
Output is correct |
55 |
Correct |
712 ms |
49180 KB |
Output is correct |