#include <bits/stdc++.h>
using namespace std;
const int N = 500'000 + 10;
int n;
int a[N];
int q;
vector<pair<int, int>> Q[N];
int f[N << 2], T[N << 2], lazy[N << 2];
void build(int s, int l, int r) {
if (l == r) {
T[s] = f[s] = a[l];
return;
}
int mid = l + r >> 1;
build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
T[s] = max(T[s << 1], T[s << 1 | 1]);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
void push(int s) {
f[s << 1] = max(f[s << 1], T[s << 1] + lazy[s]);
f[s << 1 | 1] = max(f[s << 1 | 1], T[s << 1 | 1] + lazy[s]);
lazy[s << 1] = max(lazy[s << 1], lazy[s]);
lazy[s << 1 | 1] = max(lazy[s << 1 | 1], lazy[s]);
}
void update(int s, int l, int r, int u, int v, int x) {
if (v < l || u > r) return;
if (u <= l && r <= v) {
lazy[s] = max(lazy[s], x);
f[s] = max(f[s], T[s] + x);
return;
}
push(s);
int mid = l + r >> 1;
update(s << 1, l, mid, u, v, x); update(s << 1 | 1, mid + 1, r, u, v, x);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return 0;
if (u <= l && r <= v) return f[s];
push(s);
int mid = l + r >> 1;
return max(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cin >> q;
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
Q[l].push_back({r, i});
}
vector<int>answer(q + 1);
build(1, 1, n);
stack<int> st;
for (int i = n; i >= 1; --i) {
while (st.size() && a[st.top()] <= a[i]) {
update(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
st.pop();
}
if (st.size()) update(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
for (const auto& [r, idx] : Q[i]) answer[idx] = query(1, 1, n, i, r);
st.push(i);
}
for (int i = 1; i <= q; ++i) cout << answer[i] << "\n";
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:17:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int query(int, int, int, int, int)':
jumps.cpp:46:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
18780 KB |
Output is correct |
2 |
Correct |
5 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
18780 KB |
Output is correct |
2 |
Correct |
5 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
250 ms |
32852 KB |
Output is correct |
12 |
Correct |
227 ms |
36636 KB |
Output is correct |
13 |
Correct |
215 ms |
36536 KB |
Output is correct |
14 |
Correct |
220 ms |
36820 KB |
Output is correct |
15 |
Correct |
263 ms |
36724 KB |
Output is correct |
16 |
Correct |
219 ms |
36104 KB |
Output is correct |
17 |
Correct |
217 ms |
35872 KB |
Output is correct |
18 |
Correct |
215 ms |
35788 KB |
Output is correct |
19 |
Correct |
217 ms |
36584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
24908 KB |
Output is correct |
2 |
Correct |
63 ms |
27220 KB |
Output is correct |
3 |
Correct |
65 ms |
26412 KB |
Output is correct |
4 |
Correct |
99 ms |
26600 KB |
Output is correct |
5 |
Correct |
100 ms |
26488 KB |
Output is correct |
6 |
Correct |
94 ms |
26092 KB |
Output is correct |
7 |
Correct |
97 ms |
26148 KB |
Output is correct |
8 |
Correct |
97 ms |
25824 KB |
Output is correct |
9 |
Correct |
95 ms |
26384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
18780 KB |
Output is correct |
2 |
Correct |
5 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
250 ms |
32852 KB |
Output is correct |
12 |
Correct |
227 ms |
36636 KB |
Output is correct |
13 |
Correct |
215 ms |
36536 KB |
Output is correct |
14 |
Correct |
220 ms |
36820 KB |
Output is correct |
15 |
Correct |
263 ms |
36724 KB |
Output is correct |
16 |
Correct |
219 ms |
36104 KB |
Output is correct |
17 |
Correct |
217 ms |
35872 KB |
Output is correct |
18 |
Correct |
215 ms |
35788 KB |
Output is correct |
19 |
Correct |
217 ms |
36584 KB |
Output is correct |
20 |
Correct |
99 ms |
24908 KB |
Output is correct |
21 |
Correct |
63 ms |
27220 KB |
Output is correct |
22 |
Correct |
65 ms |
26412 KB |
Output is correct |
23 |
Correct |
99 ms |
26600 KB |
Output is correct |
24 |
Correct |
100 ms |
26488 KB |
Output is correct |
25 |
Correct |
94 ms |
26092 KB |
Output is correct |
26 |
Correct |
97 ms |
26148 KB |
Output is correct |
27 |
Correct |
97 ms |
25824 KB |
Output is correct |
28 |
Correct |
95 ms |
26384 KB |
Output is correct |
29 |
Correct |
721 ms |
56772 KB |
Output is correct |
30 |
Correct |
602 ms |
58696 KB |
Output is correct |
31 |
Correct |
584 ms |
56916 KB |
Output is correct |
32 |
Correct |
683 ms |
56644 KB |
Output is correct |
33 |
Correct |
707 ms |
57172 KB |
Output is correct |
34 |
Correct |
678 ms |
54932 KB |
Output is correct |
35 |
Correct |
677 ms |
54628 KB |
Output is correct |
36 |
Correct |
674 ms |
55084 KB |
Output is correct |
37 |
Correct |
718 ms |
55636 KB |
Output is correct |
38 |
Correct |
501 ms |
62272 KB |
Output is correct |
39 |
Correct |
498 ms |
62204 KB |
Output is correct |
40 |
Correct |
536 ms |
59512 KB |
Output is correct |
41 |
Correct |
495 ms |
59104 KB |
Output is correct |
42 |
Correct |
499 ms |
58960 KB |
Output is correct |
43 |
Correct |
497 ms |
60644 KB |
Output is correct |
44 |
Correct |
551 ms |
61548 KB |
Output is correct |
45 |
Correct |
553 ms |
61672 KB |
Output is correct |
46 |
Correct |
520 ms |
59160 KB |
Output is correct |
47 |
Correct |
512 ms |
58672 KB |
Output is correct |
48 |
Correct |
539 ms |
58584 KB |
Output is correct |
49 |
Correct |
586 ms |
60492 KB |
Output is correct |
50 |
Correct |
585 ms |
60000 KB |
Output is correct |
51 |
Correct |
592 ms |
60232 KB |
Output is correct |
52 |
Correct |
577 ms |
57764 KB |
Output is correct |
53 |
Correct |
575 ms |
57820 KB |
Output is correct |
54 |
Correct |
588 ms |
57548 KB |
Output is correct |
55 |
Correct |
591 ms |
59056 KB |
Output is correct |