#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 500005, Q = 500005;
int n, q, l, r, a[N], ans[Q];
vector<int> pos;
vector<pair<int, int>> upd[N], que[N];
struct STree
{
#define m (l + r) / 2
#define lc i * 2
#define rc i * 2 + 1
int tot[4 * N], cur[4 * N] , lz[4 * N];
void apply(int i, int v)
{
lz[i] = max(lz[i], v);
tot[i] = max(tot[i], cur[i] + lz[i]);
}
void down(int i)
{
apply(lc, lz[i]);
apply(rc, lz[i]);
lz[i] = 0;
}
void update_range(int l, int r, int i, int L, int R, int v)
{
if (l > R || r < L || L > R)
return;
if (L <= l && r <= R)
{
apply(i, v);
return;
}
down(i);
update_range(l, m, lc, L, R, v);
update_range(m + 1, r, rc, L, R, v);
tot[i] = max(tot[lc], tot[rc]);
}
void update_node(int l, int r, int i, int u, int v)
{
if (l > u || r < u)
return;
if (l == r)
{
cur[i] = v; lz[i] = 0;
apply(i, 0);
return;
}
down(i);
if (u <= m)
update_node(l, m, lc, u, v);
else
update_node(m + 1, r, rc, u, v);
cur[i] = max(cur[lc], cur[rc]);
tot[i] = max(tot[lc], tot[rc]);
}
int query(int l, int r, int i, int L, int R)
{
if (l > R || r < L || L > R)
return 0;
else if (L <= l && r <= R)
return tot[i];
down(i);
return max(query(l, m, lc, L, R), query(m + 1, r, rc, L, R));
}
#undef m
#undef lc
#undef rc
} seg;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
while (!pos.empty())
{
int u = pos.back();
if (2 * i - u <= n)
upd[2 * i - u].push_back({u, a[i] + a[u]});
if (a[u] <= a[i])
pos.pop_back();
else
break;
}
pos.push_back(i);
}
cin >> q;
for (int i = 1; i <= q; i++)
{
cin >> l >> r;
que[r].push_back({l, i});
}
for (int i = 1; i <= n; i++)
{
for (pair<int, int> &v : upd[i])
seg.update_node(1, n, 1, v.fi, v.se);
seg.update_range(1, n, 1, 1, i, a[i]);
for (pair<int, int> &v : que[i])
ans[v.se] = seg.query(1, n, 1, v.fi, i);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23684 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23860 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23684 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23860 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
358 ms |
37680 KB |
Output is correct |
12 |
Correct |
382 ms |
37624 KB |
Output is correct |
13 |
Correct |
385 ms |
37620 KB |
Output is correct |
14 |
Correct |
349 ms |
37608 KB |
Output is correct |
15 |
Correct |
353 ms |
37496 KB |
Output is correct |
16 |
Correct |
361 ms |
36968 KB |
Output is correct |
17 |
Correct |
366 ms |
37112 KB |
Output is correct |
18 |
Correct |
362 ms |
36844 KB |
Output is correct |
19 |
Correct |
356 ms |
37532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
37752 KB |
Output is correct |
2 |
Correct |
152 ms |
36984 KB |
Output is correct |
3 |
Correct |
154 ms |
37988 KB |
Output is correct |
4 |
Correct |
209 ms |
37752 KB |
Output is correct |
5 |
Correct |
210 ms |
37852 KB |
Output is correct |
6 |
Correct |
203 ms |
37884 KB |
Output is correct |
7 |
Correct |
203 ms |
37752 KB |
Output is correct |
8 |
Correct |
201 ms |
37752 KB |
Output is correct |
9 |
Correct |
205 ms |
37752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23684 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23860 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
358 ms |
37680 KB |
Output is correct |
12 |
Correct |
382 ms |
37624 KB |
Output is correct |
13 |
Correct |
385 ms |
37620 KB |
Output is correct |
14 |
Correct |
349 ms |
37608 KB |
Output is correct |
15 |
Correct |
353 ms |
37496 KB |
Output is correct |
16 |
Correct |
361 ms |
36968 KB |
Output is correct |
17 |
Correct |
366 ms |
37112 KB |
Output is correct |
18 |
Correct |
362 ms |
36844 KB |
Output is correct |
19 |
Correct |
356 ms |
37532 KB |
Output is correct |
20 |
Correct |
210 ms |
37752 KB |
Output is correct |
21 |
Correct |
152 ms |
36984 KB |
Output is correct |
22 |
Correct |
154 ms |
37988 KB |
Output is correct |
23 |
Correct |
209 ms |
37752 KB |
Output is correct |
24 |
Correct |
210 ms |
37852 KB |
Output is correct |
25 |
Correct |
203 ms |
37884 KB |
Output is correct |
26 |
Correct |
203 ms |
37752 KB |
Output is correct |
27 |
Correct |
201 ms |
37752 KB |
Output is correct |
28 |
Correct |
205 ms |
37752 KB |
Output is correct |
29 |
Correct |
1223 ms |
72592 KB |
Output is correct |
30 |
Correct |
1106 ms |
81564 KB |
Output is correct |
31 |
Correct |
1081 ms |
83504 KB |
Output is correct |
32 |
Correct |
1224 ms |
83600 KB |
Output is correct |
33 |
Correct |
1213 ms |
83704 KB |
Output is correct |
34 |
Correct |
1292 ms |
81428 KB |
Output is correct |
35 |
Correct |
1348 ms |
81012 KB |
Output is correct |
36 |
Correct |
1250 ms |
81084 KB |
Output is correct |
37 |
Correct |
1221 ms |
82432 KB |
Output is correct |
38 |
Correct |
910 ms |
89468 KB |
Output is correct |
39 |
Correct |
915 ms |
89280 KB |
Output is correct |
40 |
Correct |
896 ms |
86012 KB |
Output is correct |
41 |
Correct |
885 ms |
85496 KB |
Output is correct |
42 |
Correct |
890 ms |
85372 KB |
Output is correct |
43 |
Correct |
893 ms |
87128 KB |
Output is correct |
44 |
Correct |
984 ms |
88528 KB |
Output is correct |
45 |
Correct |
962 ms |
88568 KB |
Output is correct |
46 |
Correct |
945 ms |
85692 KB |
Output is correct |
47 |
Correct |
945 ms |
85240 KB |
Output is correct |
48 |
Correct |
944 ms |
85144 KB |
Output is correct |
49 |
Correct |
970 ms |
87276 KB |
Output is correct |
50 |
Correct |
1047 ms |
86776 KB |
Output is correct |
51 |
Correct |
1045 ms |
86904 KB |
Output is correct |
52 |
Correct |
1049 ms |
84600 KB |
Output is correct |
53 |
Correct |
1029 ms |
84008 KB |
Output is correct |
54 |
Correct |
1020 ms |
84132 KB |
Output is correct |
55 |
Correct |
1034 ms |
85692 KB |
Output is correct |