#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define s second
#define f first
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 5e5 + 10;
struct node{
ll a = 0, b = 0;
};
ll a[N];
node seg[N * 4];
ll laz[N * 4];
void build(int id, int l, int r) {
if (l == r) {
seg[id].a = a[l];
seg[id].b = a[l];
return;
}
int m = (l + r) / 2;
build(id * 2, l, m);
build(id * 2 + 1, m + 1, r);
seg[id].a = max(seg[id * 2].a, seg[id * 2 + 1].a);
seg[id].b = max(seg[id * 2].b, seg[id * 2 + 1].b);
}
void push(int id) {
ll t = laz[id];
laz[id * 2] = max(laz[id * 2], t);
laz[id * 2 + 1] = max(laz[id * 2 + 1], t);
seg[id * 2].b = max(seg[id * 2].a + t, seg[id * 2].b);
seg[id * 2 + 1].b = max(seg[id * 2 + 1].a + t,seg[id * 2 + 1].b);
}
void upd(int id, int l, int r, int u, int v, ll val) {
if (l > v || r < u)
return;
if (l >= u && r <= v) {
laz[id] = max(laz[id], val);
seg[id].b = max(seg[id].b, seg[id].a + val);
return;
}
push(id);
int m = (l + r) / 2;
upd(id * 2, l, m, u, v, val);
upd(id * 2 + 1, m + 1, r, u, v, val);
seg[id].a = max(seg[id * 2].a, seg[id * 2 + 1].a);
seg[id].b = max(seg[id * 2].b, seg[id * 2 + 1].b);
}
ll get(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return 0;
if (l >= u && r <= v)
return seg[id].b;
push(id);
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
vector <int> change[N];
vector <pair <int, int>> ask[N];
ll ans[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
stack <int> st;
for (int i = n; i >= 1; i--) {
while (sz(st)) {
int t = st.top();
change[i].pb(t);
if (a[i] <= a[t])
break;
st.pop();
}
st.push(i);
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
ask[l].pb({r, i});
}
for (int i = n; i >= 1; i--) {
for (auto x : change[i]) {
if (x * 2 - i <= n) {
upd(1, 1, n, x * 2 - i, n, a[i] + a[x]);
}
}
for (auto x : ask[i]) {
ans[x.s] = max(ans[x.s], get(1, 1, n, i, x.f));
}
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
return 0;
}
// b - a <= c - b
// tinh so o (x, y) sao cho
// min(abs(x - x1), abs(y - y1))
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
26968 KB |
Output is correct |
4 |
Correct |
6 ms |
26968 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
6 ms |
27092 KB |
Output is correct |
7 |
Correct |
8 ms |
26972 KB |
Output is correct |
8 |
Correct |
6 ms |
26972 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
7 ms |
27072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
26968 KB |
Output is correct |
4 |
Correct |
6 ms |
26968 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
6 ms |
27092 KB |
Output is correct |
7 |
Correct |
8 ms |
26972 KB |
Output is correct |
8 |
Correct |
6 ms |
26972 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
7 ms |
27072 KB |
Output is correct |
11 |
Correct |
215 ms |
46420 KB |
Output is correct |
12 |
Correct |
209 ms |
46364 KB |
Output is correct |
13 |
Correct |
210 ms |
46420 KB |
Output is correct |
14 |
Correct |
203 ms |
46416 KB |
Output is correct |
15 |
Correct |
204 ms |
46464 KB |
Output is correct |
16 |
Correct |
193 ms |
45700 KB |
Output is correct |
17 |
Correct |
213 ms |
45812 KB |
Output is correct |
18 |
Correct |
210 ms |
45660 KB |
Output is correct |
19 |
Correct |
211 ms |
46164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
47696 KB |
Output is correct |
2 |
Correct |
74 ms |
48464 KB |
Output is correct |
3 |
Correct |
75 ms |
47440 KB |
Output is correct |
4 |
Correct |
121 ms |
47512 KB |
Output is correct |
5 |
Correct |
119 ms |
47956 KB |
Output is correct |
6 |
Correct |
114 ms |
47084 KB |
Output is correct |
7 |
Correct |
128 ms |
46928 KB |
Output is correct |
8 |
Correct |
120 ms |
46884 KB |
Output is correct |
9 |
Correct |
115 ms |
47304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
26968 KB |
Output is correct |
4 |
Correct |
6 ms |
26968 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
6 ms |
27092 KB |
Output is correct |
7 |
Correct |
8 ms |
26972 KB |
Output is correct |
8 |
Correct |
6 ms |
26972 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
7 ms |
27072 KB |
Output is correct |
11 |
Correct |
215 ms |
46420 KB |
Output is correct |
12 |
Correct |
209 ms |
46364 KB |
Output is correct |
13 |
Correct |
210 ms |
46420 KB |
Output is correct |
14 |
Correct |
203 ms |
46416 KB |
Output is correct |
15 |
Correct |
204 ms |
46464 KB |
Output is correct |
16 |
Correct |
193 ms |
45700 KB |
Output is correct |
17 |
Correct |
213 ms |
45812 KB |
Output is correct |
18 |
Correct |
210 ms |
45660 KB |
Output is correct |
19 |
Correct |
211 ms |
46164 KB |
Output is correct |
20 |
Correct |
115 ms |
47696 KB |
Output is correct |
21 |
Correct |
74 ms |
48464 KB |
Output is correct |
22 |
Correct |
75 ms |
47440 KB |
Output is correct |
23 |
Correct |
121 ms |
47512 KB |
Output is correct |
24 |
Correct |
119 ms |
47956 KB |
Output is correct |
25 |
Correct |
114 ms |
47084 KB |
Output is correct |
26 |
Correct |
128 ms |
46928 KB |
Output is correct |
27 |
Correct |
120 ms |
46884 KB |
Output is correct |
28 |
Correct |
115 ms |
47304 KB |
Output is correct |
29 |
Correct |
723 ms |
98384 KB |
Output is correct |
30 |
Correct |
632 ms |
99880 KB |
Output is correct |
31 |
Correct |
574 ms |
97876 KB |
Output is correct |
32 |
Correct |
713 ms |
98644 KB |
Output is correct |
33 |
Correct |
708 ms |
98420 KB |
Output is correct |
34 |
Correct |
705 ms |
96080 KB |
Output is correct |
35 |
Correct |
699 ms |
95828 KB |
Output is correct |
36 |
Correct |
696 ms |
95836 KB |
Output is correct |
37 |
Correct |
701 ms |
97260 KB |
Output is correct |
38 |
Correct |
517 ms |
104276 KB |
Output is correct |
39 |
Correct |
523 ms |
104308 KB |
Output is correct |
40 |
Correct |
516 ms |
100872 KB |
Output is correct |
41 |
Correct |
493 ms |
100180 KB |
Output is correct |
42 |
Correct |
503 ms |
100180 KB |
Output is correct |
43 |
Correct |
529 ms |
102048 KB |
Output is correct |
44 |
Correct |
558 ms |
103424 KB |
Output is correct |
45 |
Correct |
527 ms |
103404 KB |
Output is correct |
46 |
Correct |
528 ms |
100436 KB |
Output is correct |
47 |
Correct |
519 ms |
100024 KB |
Output is correct |
48 |
Correct |
550 ms |
99924 KB |
Output is correct |
49 |
Correct |
514 ms |
101972 KB |
Output is correct |
50 |
Correct |
579 ms |
101712 KB |
Output is correct |
51 |
Correct |
607 ms |
101568 KB |
Output is correct |
52 |
Correct |
618 ms |
99156 KB |
Output is correct |
53 |
Correct |
595 ms |
99052 KB |
Output is correct |
54 |
Correct |
588 ms |
98896 KB |
Output is correct |
55 |
Correct |
573 ms |
100348 KB |
Output is correct |