#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 5e5 + 5;
int n, nq, a[N], t[4 * N], lazy[4 * N], mx[4 * N], f[4 * N], ans[N];
vector <int> opt[N];
vector <pair<int, ii> > q;
stack <int> st;
void build(int i, int l, int r) {
if(l == r) {
mx[i] = a[l];
return ;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
mx[i] = max(mx[i << 1], mx[i << 1 | 1]);
}
void down(int i) {
if(!down[i]) return ;
t[i << 1] = max(t[i << 1], lazy[i]);
f[i << 1] = max(f[i << 1], t[i << 1] + mx[i << 1]);
lazy[i << 1] = max(lazy[i << 1], lazy[i]);
t[i << 1 | 1] = max(t[i << 1 | 1], lazy[i]);
f[i << 1 | 1] = max(f[i << 1 | 1], t[i << 1 | 1] + mx[i << 1 | 1]);
lazy[i << 1 | 1] = max(lazy[i << 1 | 1], lazy[i]);
lazy[i] = 0;
}
void upd(int i, int l, int r, int u, int v, int val) {
if(l > v || u > r) return ;
if(u <= l && r <= v) {
t[i] = max(t[i], val);
lazy[i] = max(lazy[i], val);
f[i] = max(f[i], t[i] + mx[i]);
return ;
}
down(i);
int mid = l + r >> 1;
upd(i << 1, l, mid, u, v, val);
upd(i << 1 | 1, mid + 1, r, u, v, val);
f[i] = max(f[i << 1], f[i << 1 | 1]);
}
int get(int i, int l, int r, int u, int v) {
if(l > v || u > r) return 0;
if(u <= l && r <= v) return f[i];
down(i);
int mid = l + r >> 1;
return max(get(i << 1, l, mid, u, v) , get(i << 1 | 1, mid + 1, r, u, v));
}
main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("wa.out", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
for(int i = n; i >= 1; i--) {
while (!st.empty() && a[i] > a[st.top()]) {
opt[i].push_back(st.top());
st.pop();
}
if(!st.empty()) opt[i].push_back(st.top());
st.push(i);
}
cin >> nq;
for(int i = 1; i <= nq; i++) {
int l, r; cin >> l >> r;
q.push_back({i, {l, r}});
}
sort(q.begin(), q.end(), [] (pair<int, ii> x, pair<int, ii> y) {
return x.se.fi > y.se.fi;
});
int it = n;
for(int j = 0; j < nq; j++) {
int i = q[j].fi, l = q[j].se.fi, r = q[j].se.se;
while (it >= l) {
for(int b : opt[it]) upd(1, 1, n, 2 * b - it, n, a[it] + a[b]);
it--;
}
ans[i] = get(1, 1, n, l, r);
}
for(int i = 1; i <= nq; i++) cout << ans[i] << "\n";
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:18:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void down(int)':
jumps.cpp:25:15: warning: pointer to a function used in arithmetic [-Wpointer-arith]
25 | if(!down[i]) return ;
| ^
jumps.cpp: In function 'void upd(int, int, int, int, int, int)':
jumps.cpp:44:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get(int, int, int, int, int)':
jumps.cpp:54:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: At global scope:
jumps.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main () {
| ^~~~
jumps.cpp: In function 'int main()':
jumps.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen("wa.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
7 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12068 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
7 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12068 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
310 ms |
30136 KB |
Output is correct |
12 |
Correct |
294 ms |
30088 KB |
Output is correct |
13 |
Correct |
287 ms |
30092 KB |
Output is correct |
14 |
Correct |
297 ms |
30180 KB |
Output is correct |
15 |
Correct |
304 ms |
30176 KB |
Output is correct |
16 |
Correct |
294 ms |
29472 KB |
Output is correct |
17 |
Correct |
323 ms |
29484 KB |
Output is correct |
18 |
Correct |
335 ms |
29452 KB |
Output is correct |
19 |
Correct |
336 ms |
30116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
29220 KB |
Output is correct |
2 |
Correct |
99 ms |
29836 KB |
Output is correct |
3 |
Correct |
101 ms |
29000 KB |
Output is correct |
4 |
Correct |
157 ms |
29276 KB |
Output is correct |
5 |
Correct |
152 ms |
29340 KB |
Output is correct |
6 |
Correct |
151 ms |
28648 KB |
Output is correct |
7 |
Correct |
151 ms |
28476 KB |
Output is correct |
8 |
Correct |
161 ms |
28580 KB |
Output is correct |
9 |
Correct |
149 ms |
28876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
7 ms |
12072 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12068 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
310 ms |
30136 KB |
Output is correct |
12 |
Correct |
294 ms |
30088 KB |
Output is correct |
13 |
Correct |
287 ms |
30092 KB |
Output is correct |
14 |
Correct |
297 ms |
30180 KB |
Output is correct |
15 |
Correct |
304 ms |
30176 KB |
Output is correct |
16 |
Correct |
294 ms |
29472 KB |
Output is correct |
17 |
Correct |
323 ms |
29484 KB |
Output is correct |
18 |
Correct |
335 ms |
29452 KB |
Output is correct |
19 |
Correct |
336 ms |
30116 KB |
Output is correct |
20 |
Correct |
156 ms |
29220 KB |
Output is correct |
21 |
Correct |
99 ms |
29836 KB |
Output is correct |
22 |
Correct |
101 ms |
29000 KB |
Output is correct |
23 |
Correct |
157 ms |
29276 KB |
Output is correct |
24 |
Correct |
152 ms |
29340 KB |
Output is correct |
25 |
Correct |
151 ms |
28648 KB |
Output is correct |
26 |
Correct |
151 ms |
28476 KB |
Output is correct |
27 |
Correct |
161 ms |
28580 KB |
Output is correct |
28 |
Correct |
149 ms |
28876 KB |
Output is correct |
29 |
Correct |
976 ms |
70556 KB |
Output is correct |
30 |
Correct |
760 ms |
72024 KB |
Output is correct |
31 |
Correct |
749 ms |
69984 KB |
Output is correct |
32 |
Correct |
920 ms |
70608 KB |
Output is correct |
33 |
Correct |
907 ms |
70716 KB |
Output is correct |
34 |
Correct |
896 ms |
68260 KB |
Output is correct |
35 |
Correct |
962 ms |
68028 KB |
Output is correct |
36 |
Correct |
893 ms |
68064 KB |
Output is correct |
37 |
Correct |
940 ms |
69520 KB |
Output is correct |
38 |
Correct |
751 ms |
70516 KB |
Output is correct |
39 |
Correct |
758 ms |
70700 KB |
Output is correct |
40 |
Correct |
727 ms |
67172 KB |
Output is correct |
41 |
Correct |
726 ms |
66752 KB |
Output is correct |
42 |
Correct |
724 ms |
66704 KB |
Output is correct |
43 |
Correct |
731 ms |
68516 KB |
Output is correct |
44 |
Correct |
741 ms |
70608 KB |
Output is correct |
45 |
Correct |
777 ms |
70540 KB |
Output is correct |
46 |
Correct |
763 ms |
67420 KB |
Output is correct |
47 |
Correct |
795 ms |
66960 KB |
Output is correct |
48 |
Correct |
845 ms |
67048 KB |
Output is correct |
49 |
Correct |
855 ms |
69084 KB |
Output is correct |
50 |
Correct |
1030 ms |
70704 KB |
Output is correct |
51 |
Correct |
805 ms |
70636 KB |
Output is correct |
52 |
Correct |
827 ms |
68092 KB |
Output is correct |
53 |
Correct |
804 ms |
67840 KB |
Output is correct |
54 |
Correct |
830 ms |
67852 KB |
Output is correct |
55 |
Correct |
791 ms |
69540 KB |
Output is correct |