#include <bits/stdc++.h>
#define int long long
using namespace std;
const int O = 5e5 + 5;
int n, q, l[O], r[O], a[O], res[O], tree[O * 4], init[O * 4], lazy[O * 4];
vector <int> qu[O], nxt[O];
void Push(int id){
int &x = lazy[id];
if (x == 0) return;
for (int i = 2 * id; i <= 2 * id + 1; ++ i){
tree[i] = max(tree[i], init[i] + x);
lazy[i] = max(lazy[i], x);
}
x = 0; return;
}
void Build(int id, int l, int r){
if (l == r){
init[id] = a[l];
return;
}
int mid = (l + r) / 2;
Build(id << 1, l, mid);
Build(id << 1 | 1, mid + 1, r);
init[id] = max(init[id << 1], init[id << 1 | 1]);
}
void Update(int id, int l, int r, int u, int v, int x){
if (l > v || r < u) return;
if (u <= l && r <= v){
tree[id] = max(tree[id], init[id] + x);
lazy[id] = max(lazy[id], x);
return;
}
int mid = (l + r) / 2; Push(id);
Update(id << 1, l, mid, u, v, x);
Update(id << 1 | 1, mid + 1, r, u, v, x);
tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
}
int Get(int id, int l, int r, int u, int v){
if (l > v || r < u) return 0;
if (u <= l && r <= v) return tree[id];
int mid = (l + r) / 2; Push(id);
return max(Get(id << 1, l, mid, u, v), Get(id << 1 | 1, mid + 1, r, u, v));
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++ i) cin >> a[i];
Build(1, 1, n);
cin >> q;
for (int i = 1; i <= q; ++ i){
cin >> l[i] >> r[i];
qu[l[i]].push_back(i);
}
vector <int> Q;
for (int i = 1; i <= n; ++ i){
while (Q.size() && a[Q.back()] <= a[i]){
nxt[Q.back()].push_back(i);
Q.pop_back();
}
if (Q.size()) nxt[Q.back()].push_back(i);
Q.push_back(i);
}
for (int i = n; i >= 1; -- i){
for (int j : nxt[i]){
Update(1, 1, n, 2 * j - i, n, a[i] + a[j]);
}
for (int j : qu[i]){
res[j] = Get(1, 1, n, i, r[j]);
}
}
for (int i = 1; i <= q; ++ i) cout << res[i] << "\n";
}
Compilation message
jumps.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
54 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23852 KB |
Output is correct |
4 |
Correct |
16 ms |
23768 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
13 ms |
23804 KB |
Output is correct |
7 |
Correct |
15 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23852 KB |
Output is correct |
4 |
Correct |
16 ms |
23768 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
13 ms |
23804 KB |
Output is correct |
7 |
Correct |
15 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
323 ms |
51624 KB |
Output is correct |
12 |
Correct |
291 ms |
51916 KB |
Output is correct |
13 |
Correct |
285 ms |
52180 KB |
Output is correct |
14 |
Correct |
270 ms |
51916 KB |
Output is correct |
15 |
Correct |
290 ms |
51972 KB |
Output is correct |
16 |
Correct |
310 ms |
51296 KB |
Output is correct |
17 |
Correct |
294 ms |
51364 KB |
Output is correct |
18 |
Correct |
290 ms |
51208 KB |
Output is correct |
19 |
Correct |
337 ms |
51884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
45732 KB |
Output is correct |
2 |
Correct |
92 ms |
44684 KB |
Output is correct |
3 |
Correct |
90 ms |
46384 KB |
Output is correct |
4 |
Correct |
144 ms |
45940 KB |
Output is correct |
5 |
Correct |
155 ms |
46128 KB |
Output is correct |
6 |
Correct |
156 ms |
46100 KB |
Output is correct |
7 |
Correct |
138 ms |
46056 KB |
Output is correct |
8 |
Correct |
143 ms |
46128 KB |
Output is correct |
9 |
Correct |
159 ms |
46608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23804 KB |
Output is correct |
3 |
Correct |
13 ms |
23852 KB |
Output is correct |
4 |
Correct |
16 ms |
23768 KB |
Output is correct |
5 |
Correct |
13 ms |
23800 KB |
Output is correct |
6 |
Correct |
13 ms |
23804 KB |
Output is correct |
7 |
Correct |
15 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
323 ms |
51624 KB |
Output is correct |
12 |
Correct |
291 ms |
51916 KB |
Output is correct |
13 |
Correct |
285 ms |
52180 KB |
Output is correct |
14 |
Correct |
270 ms |
51916 KB |
Output is correct |
15 |
Correct |
290 ms |
51972 KB |
Output is correct |
16 |
Correct |
310 ms |
51296 KB |
Output is correct |
17 |
Correct |
294 ms |
51364 KB |
Output is correct |
18 |
Correct |
290 ms |
51208 KB |
Output is correct |
19 |
Correct |
337 ms |
51884 KB |
Output is correct |
20 |
Correct |
152 ms |
45732 KB |
Output is correct |
21 |
Correct |
92 ms |
44684 KB |
Output is correct |
22 |
Correct |
90 ms |
46384 KB |
Output is correct |
23 |
Correct |
144 ms |
45940 KB |
Output is correct |
24 |
Correct |
155 ms |
46128 KB |
Output is correct |
25 |
Correct |
156 ms |
46100 KB |
Output is correct |
26 |
Correct |
138 ms |
46056 KB |
Output is correct |
27 |
Correct |
143 ms |
46128 KB |
Output is correct |
28 |
Correct |
159 ms |
46608 KB |
Output is correct |
29 |
Correct |
1020 ms |
108632 KB |
Output is correct |
30 |
Correct |
858 ms |
105576 KB |
Output is correct |
31 |
Correct |
885 ms |
109492 KB |
Output is correct |
32 |
Correct |
1042 ms |
108792 KB |
Output is correct |
33 |
Correct |
1032 ms |
108696 KB |
Output is correct |
34 |
Correct |
991 ms |
106424 KB |
Output is correct |
35 |
Correct |
1008 ms |
106052 KB |
Output is correct |
36 |
Correct |
995 ms |
106204 KB |
Output is correct |
37 |
Correct |
1057 ms |
107608 KB |
Output is correct |
38 |
Correct |
690 ms |
114308 KB |
Output is correct |
39 |
Correct |
638 ms |
114328 KB |
Output is correct |
40 |
Correct |
611 ms |
111176 KB |
Output is correct |
41 |
Correct |
625 ms |
110468 KB |
Output is correct |
42 |
Correct |
637 ms |
110484 KB |
Output is correct |
43 |
Correct |
630 ms |
112284 KB |
Output is correct |
44 |
Correct |
697 ms |
113796 KB |
Output is correct |
45 |
Correct |
680 ms |
113684 KB |
Output is correct |
46 |
Correct |
661 ms |
110600 KB |
Output is correct |
47 |
Correct |
660 ms |
110168 KB |
Output is correct |
48 |
Correct |
671 ms |
110116 KB |
Output is correct |
49 |
Correct |
679 ms |
112284 KB |
Output is correct |
50 |
Correct |
759 ms |
111652 KB |
Output is correct |
51 |
Correct |
766 ms |
111576 KB |
Output is correct |
52 |
Correct |
713 ms |
109188 KB |
Output is correct |
53 |
Correct |
765 ms |
108808 KB |
Output is correct |
54 |
Correct |
758 ms |
108868 KB |
Output is correct |
55 |
Correct |
731 ms |
110556 KB |
Output is correct |