#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int a[N];
vector<pair<int, int>> que[N];
int resenje[N];
int b[N];
pair<int, int> c[N];
int seg[4 * N - 1][2];
int lazy[4 * N - 1];
int n;
int ind = 0;
void build(int u, int l, int r) {
if (l == r) {
seg[u][1] = a[l];
return;
}
int mid = (l + r) >> 1;
build(u * 2 + 1, l, mid);
build(u * 2 + 2, mid + 1, r);
seg[u][1] = max(seg[u * 2 + 1][1], seg[u * 2 + 2][1]);
}
void upd(int u, int x) {
seg[u][0] = max(seg[u][0], x + seg[u][1]);
lazy[u] = max(lazy[u], x);
}
void shift(int u) {
if (!lazy[u])
return;
upd(u * 2 + 1, lazy[u]);
upd(u * 2 + 2, lazy[u]);
lazy[u] = 0;
}
void update(int u, int l, int r, int L, int R, int x) {
if (r < L || l > R)
return;
if (l >= L && r <= R) {
upd(u, x);
return;
}
shift(u);
int mid = (l + r) >> 1;
update(u * 2 + 1, l, mid, L, R, x);
update(u * 2 + 2, mid + 1, r, L, R, x);
seg[u][0] = max(seg[u * 2 + 1][0], seg[u * 2 + 2][0]);
}
void Update(int i) {
if (b[i]) {
int l = 2 * b[i] - i;
if (l < n) {
update(0, 0, n - 1, l, n - 1, a[i] + a[b[i]]);
}
}
while (c[ind].first == i) {
int l = 2 * c[ind].second - c[ind].first;
if (l < n) {
update(0, 0, n - 1, l, n - 1, a[c[ind].first] + a[c[ind].second]);
}
ind++;
}
}
int out(int u, int l, int r, int L, int R) {
if (r < L || l > R)
return 0;
if (l >= L && r <= R) {
return seg[u][0];
}
shift(u);
int mid = (l + r) >> 1;
return max(out(u * 2 + 1, l, mid, L, R), out(u * 2 + 2, mid + 1, r, L, R));
}
void veca(int n) {
stack<int> s;
for (int i = 0; i < n; i++) {
while (!s.empty() && a[s.top()] <= a[i]) {
b[s.top()] = i;
s.pop();
}
s.push(i);
}
}
void veca1(int n) {
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && a[s.top()] <= a[i]) {
c[ind].first = i;
c[ind++].second = s.top();
s.pop();
}
s.push(i);
}
c[ind].first = -1;
ind = 0;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return a.first.first >= b.first.first;
}
void solve() {
int q;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
que[l - 1].push_back({r - 1, i});
}
veca(n);
veca1(n);
build(0, 0, n - 1);
for (int i = n - 1; i >= 0; i--) {
Update(i);
for (auto j = que[i].begin(); j != que[i].end(); j++) {
pair<int, int> par = *j;
resenje[par.second] = out(0, 0, n - 1, i, par.first);
}
}
for (int i = 0; i < q; i++)
cout << resenje[i] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
1411 ms |
26376 KB |
Output is correct |
12 |
Correct |
1379 ms |
26732 KB |
Output is correct |
13 |
Correct |
1373 ms |
26476 KB |
Output is correct |
14 |
Correct |
1366 ms |
26348 KB |
Output is correct |
15 |
Correct |
1433 ms |
26332 KB |
Output is correct |
16 |
Correct |
1372 ms |
25708 KB |
Output is correct |
17 |
Correct |
1389 ms |
25836 KB |
Output is correct |
18 |
Correct |
1362 ms |
25836 KB |
Output is correct |
19 |
Correct |
1381 ms |
26364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
21440 KB |
Output is correct |
2 |
Correct |
110 ms |
20160 KB |
Output is correct |
3 |
Correct |
96 ms |
21184 KB |
Output is correct |
4 |
Correct |
158 ms |
21356 KB |
Output is correct |
5 |
Correct |
176 ms |
21484 KB |
Output is correct |
6 |
Correct |
156 ms |
21356 KB |
Output is correct |
7 |
Correct |
161 ms |
21484 KB |
Output is correct |
8 |
Correct |
155 ms |
21356 KB |
Output is correct |
9 |
Correct |
160 ms |
21356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12268 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
1411 ms |
26376 KB |
Output is correct |
12 |
Correct |
1379 ms |
26732 KB |
Output is correct |
13 |
Correct |
1373 ms |
26476 KB |
Output is correct |
14 |
Correct |
1366 ms |
26348 KB |
Output is correct |
15 |
Correct |
1433 ms |
26332 KB |
Output is correct |
16 |
Correct |
1372 ms |
25708 KB |
Output is correct |
17 |
Correct |
1389 ms |
25836 KB |
Output is correct |
18 |
Correct |
1362 ms |
25836 KB |
Output is correct |
19 |
Correct |
1381 ms |
26364 KB |
Output is correct |
20 |
Correct |
174 ms |
21440 KB |
Output is correct |
21 |
Correct |
110 ms |
20160 KB |
Output is correct |
22 |
Correct |
96 ms |
21184 KB |
Output is correct |
23 |
Correct |
158 ms |
21356 KB |
Output is correct |
24 |
Correct |
176 ms |
21484 KB |
Output is correct |
25 |
Correct |
156 ms |
21356 KB |
Output is correct |
26 |
Correct |
161 ms |
21484 KB |
Output is correct |
27 |
Correct |
155 ms |
21356 KB |
Output is correct |
28 |
Correct |
160 ms |
21356 KB |
Output is correct |
29 |
Correct |
2227 ms |
49260 KB |
Output is correct |
30 |
Correct |
2040 ms |
56440 KB |
Output is correct |
31 |
Correct |
2063 ms |
60072 KB |
Output is correct |
32 |
Correct |
2218 ms |
60156 KB |
Output is correct |
33 |
Correct |
2221 ms |
60140 KB |
Output is correct |
34 |
Correct |
2197 ms |
57836 KB |
Output is correct |
35 |
Correct |
2207 ms |
57580 KB |
Output is correct |
36 |
Correct |
2196 ms |
57580 KB |
Output is correct |
37 |
Correct |
2212 ms |
59040 KB |
Output is correct |
38 |
Correct |
1862 ms |
65772 KB |
Output is correct |
39 |
Correct |
1827 ms |
65772 KB |
Output is correct |
40 |
Correct |
1827 ms |
62572 KB |
Output is correct |
41 |
Correct |
1806 ms |
62108 KB |
Output is correct |
42 |
Correct |
1801 ms |
61932 KB |
Output is correct |
43 |
Correct |
1807 ms |
63724 KB |
Output is correct |
44 |
Correct |
1881 ms |
65260 KB |
Output is correct |
45 |
Correct |
1891 ms |
65260 KB |
Output is correct |
46 |
Correct |
1877 ms |
62040 KB |
Output is correct |
47 |
Correct |
1864 ms |
61688 KB |
Output is correct |
48 |
Correct |
1878 ms |
61748 KB |
Output is correct |
49 |
Correct |
1868 ms |
63832 KB |
Output is correct |
50 |
Correct |
1965 ms |
63628 KB |
Output is correct |
51 |
Correct |
1980 ms |
63416 KB |
Output is correct |
52 |
Correct |
1954 ms |
61100 KB |
Output is correct |
53 |
Correct |
1931 ms |
60716 KB |
Output is correct |
54 |
Correct |
1943 ms |
60628 KB |
Output is correct |
55 |
Correct |
1953 ms |
62240 KB |
Output is correct |