#include <bits/stdc++.h>
using namespace std;
#define M 524288
int st[M << 1], A[M << 1], B[M << 1], C[M << 1], lz[M << 1];
// MIN MAX
void push(int x, int l, int r) {
if(lz[x] > 0) {
st[x] = A[x] + lz[x];
B[x] = C[x] = lz[x];
if(l != r) {
lz[x << 1] = lz[x << 1 | 1] = lz[x];
}
lz[x] = 0;
}
}
void update(int x, int l, int r, int ql, int qr, int d) {
push(x, l, r);
if(r < ql || qr < l || B[x] >= d) {
return;
}
if(ql <= l && r <= qr && C[x] <= d) {
lz[x] = d;
push(x, l, r);
}
else {
int m = (l + r) >> 1;
update(x << 1, l, m, ql, qr, d);
update(x << 1 | 1, m + 1, r, ql, qr, d);
st[x] = max(st[x << 1], st[x << 1 | 1]);
B[x] = min(B[x << 1], B[x << 1 | 1]);
C[x] = max(C[x << 1], C[x << 1 | 1]);
}
}
int query(int x, int l, int r, int ql, int qr) {
push(x, l, r);
if(r < ql || qr < l) {
return 0;
}
if(ql <= l && r <= qr) {
return st[x];
}
int m = (l + r) >> 1;
return max(query(x << 1, l, m, ql, qr), query(x << 1 | 1, m + 1, r, ql, qr));
}
#define N 500005
int a[N], L[N], R[N], res[N];
vector<int> pot[N];
vector<pair<int, int>> queries[N];
void build(int x, int l, int r) {
st[x] = B[x] = C[x] = 0;
if(l == r) {
A[x] = a[l];
}
else {
int m = (l + r) >> 1;
build(x << 1, l, m);
build(x << 1 | 1, m + 1, r);
A[x] = max(A[x << 1], A[x << 1 | 1]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 1, j; i <= n; ++i) {
j = i - 1;
while(j > 0 && a[i] > a[j]) {
j = L[j];
}
L[i] = j;
}
for(int i = n, j; i > 0; --i) {
j = i + 1;
while(j <= n && a[i] > a[j]) {
j = R[j];
}
R[i] = j;
}
for(int i = 1; i <= n; ++i) {
if(L[i] > 0) {
pot[L[i]].push_back(i);
}
if(R[i] <= n) {
pot[i].push_back(R[i]);
}
}
int q;
cin >> q;
for(int i = 1; i <= q; ++i) {
int l, r;
cin >> l >> r;
queries[l].emplace_back(i, r);
}
build(1, 1, n);
for(int i = n; i > 0; --i) {
sort(pot[i].begin(), pot[i].end());
pot[i].erase(unique(pot[i].begin(), pot[i].end()), pot[i].end());
for(int j : pot[i]) {
int k_min = j + j - i;
if(k_min <= n) {
update(1, 1, n, k_min, n, a[i] + a[j]);
}
}
for(const auto& [j, r] : queries[i]) {
res[j] = query(1, 1, n, i, r);
}
}
for(int i = 1; i <= q; ++i) {
cout << res[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23908 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
14 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23868 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23908 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
14 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23868 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
318 ms |
43016 KB |
Output is correct |
12 |
Correct |
269 ms |
42840 KB |
Output is correct |
13 |
Correct |
293 ms |
42916 KB |
Output is correct |
14 |
Correct |
278 ms |
42952 KB |
Output is correct |
15 |
Correct |
273 ms |
42980 KB |
Output is correct |
16 |
Correct |
303 ms |
42320 KB |
Output is correct |
17 |
Correct |
308 ms |
42380 KB |
Output is correct |
18 |
Correct |
387 ms |
42216 KB |
Output is correct |
19 |
Correct |
301 ms |
42860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
44672 KB |
Output is correct |
2 |
Correct |
107 ms |
43896 KB |
Output is correct |
3 |
Correct |
108 ms |
44360 KB |
Output is correct |
4 |
Correct |
212 ms |
44584 KB |
Output is correct |
5 |
Correct |
185 ms |
44632 KB |
Output is correct |
6 |
Correct |
183 ms |
43980 KB |
Output is correct |
7 |
Correct |
175 ms |
43964 KB |
Output is correct |
8 |
Correct |
186 ms |
43872 KB |
Output is correct |
9 |
Correct |
165 ms |
44212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23908 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
14 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23868 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
318 ms |
43016 KB |
Output is correct |
12 |
Correct |
269 ms |
42840 KB |
Output is correct |
13 |
Correct |
293 ms |
42916 KB |
Output is correct |
14 |
Correct |
278 ms |
42952 KB |
Output is correct |
15 |
Correct |
273 ms |
42980 KB |
Output is correct |
16 |
Correct |
303 ms |
42320 KB |
Output is correct |
17 |
Correct |
308 ms |
42380 KB |
Output is correct |
18 |
Correct |
387 ms |
42216 KB |
Output is correct |
19 |
Correct |
301 ms |
42860 KB |
Output is correct |
20 |
Correct |
172 ms |
44672 KB |
Output is correct |
21 |
Correct |
107 ms |
43896 KB |
Output is correct |
22 |
Correct |
108 ms |
44360 KB |
Output is correct |
23 |
Correct |
212 ms |
44584 KB |
Output is correct |
24 |
Correct |
185 ms |
44632 KB |
Output is correct |
25 |
Correct |
183 ms |
43980 KB |
Output is correct |
26 |
Correct |
175 ms |
43964 KB |
Output is correct |
27 |
Correct |
186 ms |
43872 KB |
Output is correct |
28 |
Correct |
165 ms |
44212 KB |
Output is correct |
29 |
Correct |
1047 ms |
94216 KB |
Output is correct |
30 |
Correct |
796 ms |
92520 KB |
Output is correct |
31 |
Correct |
886 ms |
93596 KB |
Output is correct |
32 |
Correct |
987 ms |
94176 KB |
Output is correct |
33 |
Correct |
1044 ms |
94204 KB |
Output is correct |
34 |
Correct |
1000 ms |
91936 KB |
Output is correct |
35 |
Correct |
1040 ms |
91672 KB |
Output is correct |
36 |
Correct |
1035 ms |
91648 KB |
Output is correct |
37 |
Correct |
971 ms |
93044 KB |
Output is correct |
38 |
Correct |
711 ms |
99896 KB |
Output is correct |
39 |
Correct |
716 ms |
99868 KB |
Output is correct |
40 |
Correct |
694 ms |
96540 KB |
Output is correct |
41 |
Correct |
736 ms |
96012 KB |
Output is correct |
42 |
Correct |
707 ms |
96096 KB |
Output is correct |
43 |
Correct |
695 ms |
97944 KB |
Output is correct |
44 |
Correct |
758 ms |
99256 KB |
Output is correct |
45 |
Correct |
792 ms |
99284 KB |
Output is correct |
46 |
Correct |
716 ms |
96168 KB |
Output is correct |
47 |
Correct |
747 ms |
95828 KB |
Output is correct |
48 |
Correct |
753 ms |
95820 KB |
Output is correct |
49 |
Correct |
757 ms |
97848 KB |
Output is correct |
50 |
Correct |
791 ms |
97472 KB |
Output is correct |
51 |
Correct |
812 ms |
97536 KB |
Output is correct |
52 |
Correct |
822 ms |
94956 KB |
Output is correct |
53 |
Correct |
799 ms |
94532 KB |
Output is correct |
54 |
Correct |
810 ms |
94524 KB |
Output is correct |
55 |
Correct |
796 ms |
96328 KB |
Output is correct |