#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using ll = long long;
using ii = pair<int, int>;
int N, Q, A[500005], ans[500005];
vector<int> ev[500005];
vector<ii> qu[500005];
stack<int> st;
struct node {
node *left, *right;
int S, E, r_val, A_val, pv;
node(int _s, int _e) : S(_s), E(_e), r_val(-(int)1e9), pv(-(int)1e9) {
if (S == E) {
A_val = A[S];
return;
}
int M = (S + E) / 2;
left = new node(S, M);
right = new node(M + 1, E);
A_val = max(left->A_val, right->A_val);
}
void prop() {
left->r_val = max(left->r_val, pv + left->A_val);
left->pv = max(left->pv, pv);
right->r_val = max(right->r_val, pv + right->A_val);
right->pv = max(right->pv, pv);
}
void upd(int l, int r, int v) {
if (l > E || r < S) {
return;
}
if (l <= S && E <= r) {
r_val = max(r_val, v + A_val);
pv = max(pv, v);
return;
}
prop();
left->upd(l, r, v);
right->upd(l, r, v);
r_val = max(left->r_val, right->r_val);
}
int qry(int l, int r) {
if (l > E || r < S) {
return -(int)1e9;
}
if (l <= S && E <= r) {
return r_val;
}
prop();
return max(left->qry(l, r), right->qry(l, r));
}
} *root;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
for (int i = N; i >= 1; i--) {
while (!st.empty() && A[st.top()] < A[i]) {
st.pop();
}
if (!st.empty()) {
ev[i].pb(st.top());
}
st.push(i);
}
while (!st.empty()) {
st.pop();
}
for (int i = 1; i <= N; i++) {
while (!st.empty() && A[st.top()] < A[i]) {
st.pop();
}
if (!st.empty()) {
ev[st.top()].pb(i);
}
st.push(i);
}
cin >> Q;
for (int i = 1, L, R; i <= Q; i++) {
cin >> L >> R;
qu[L].eb(R, i);
}
root = new node(1, N);
for (int i = N; i >= 1; i--) {
for (auto b : ev[i]) {
// a = i
root->upd(2 * b - i, N, A[i] + A[b]);
}
for (auto [r, idx] : qu[i]) {
ans[idx] = root->qry(1, r);
}
}
for (int i = 1; i <= Q; i++) {
cout << ans[i] << '\n';
}
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:101:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
101 | for (auto [r, idx] : qu[i]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
7 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27316 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
7 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27316 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
11 |
Correct |
170 ms |
43092 KB |
Output is correct |
12 |
Correct |
164 ms |
43856 KB |
Output is correct |
13 |
Correct |
160 ms |
43848 KB |
Output is correct |
14 |
Correct |
160 ms |
43856 KB |
Output is correct |
15 |
Correct |
157 ms |
43856 KB |
Output is correct |
16 |
Correct |
168 ms |
43600 KB |
Output is correct |
17 |
Correct |
167 ms |
43180 KB |
Output is correct |
18 |
Correct |
175 ms |
43088 KB |
Output is correct |
19 |
Correct |
159 ms |
43816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
54524 KB |
Output is correct |
2 |
Correct |
77 ms |
54060 KB |
Output is correct |
3 |
Correct |
71 ms |
54684 KB |
Output is correct |
4 |
Correct |
107 ms |
54608 KB |
Output is correct |
5 |
Correct |
111 ms |
54672 KB |
Output is correct |
6 |
Correct |
102 ms |
54020 KB |
Output is correct |
7 |
Correct |
103 ms |
53732 KB |
Output is correct |
8 |
Correct |
102 ms |
53860 KB |
Output is correct |
9 |
Correct |
105 ms |
54100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
7 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27316 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
11 |
Correct |
170 ms |
43092 KB |
Output is correct |
12 |
Correct |
164 ms |
43856 KB |
Output is correct |
13 |
Correct |
160 ms |
43848 KB |
Output is correct |
14 |
Correct |
160 ms |
43856 KB |
Output is correct |
15 |
Correct |
157 ms |
43856 KB |
Output is correct |
16 |
Correct |
168 ms |
43600 KB |
Output is correct |
17 |
Correct |
167 ms |
43180 KB |
Output is correct |
18 |
Correct |
175 ms |
43088 KB |
Output is correct |
19 |
Correct |
159 ms |
43816 KB |
Output is correct |
20 |
Correct |
105 ms |
54524 KB |
Output is correct |
21 |
Correct |
77 ms |
54060 KB |
Output is correct |
22 |
Correct |
71 ms |
54684 KB |
Output is correct |
23 |
Correct |
107 ms |
54608 KB |
Output is correct |
24 |
Correct |
111 ms |
54672 KB |
Output is correct |
25 |
Correct |
102 ms |
54020 KB |
Output is correct |
26 |
Correct |
103 ms |
53732 KB |
Output is correct |
27 |
Correct |
102 ms |
53860 KB |
Output is correct |
28 |
Correct |
105 ms |
54100 KB |
Output is correct |
29 |
Correct |
771 ms |
117024 KB |
Output is correct |
30 |
Correct |
666 ms |
116360 KB |
Output is correct |
31 |
Correct |
652 ms |
118260 KB |
Output is correct |
32 |
Correct |
780 ms |
116732 KB |
Output is correct |
33 |
Correct |
738 ms |
116744 KB |
Output is correct |
34 |
Correct |
772 ms |
114872 KB |
Output is correct |
35 |
Correct |
750 ms |
114388 KB |
Output is correct |
36 |
Correct |
759 ms |
114332 KB |
Output is correct |
37 |
Correct |
754 ms |
115796 KB |
Output is correct |
38 |
Correct |
449 ms |
122436 KB |
Output is correct |
39 |
Correct |
460 ms |
122452 KB |
Output is correct |
40 |
Correct |
448 ms |
119244 KB |
Output is correct |
41 |
Correct |
448 ms |
118612 KB |
Output is correct |
42 |
Correct |
442 ms |
118624 KB |
Output is correct |
43 |
Correct |
443 ms |
120572 KB |
Output is correct |
44 |
Correct |
481 ms |
121796 KB |
Output is correct |
45 |
Correct |
504 ms |
121840 KB |
Output is correct |
46 |
Correct |
490 ms |
118680 KB |
Output is correct |
47 |
Correct |
480 ms |
118308 KB |
Output is correct |
48 |
Correct |
476 ms |
118376 KB |
Output is correct |
49 |
Correct |
490 ms |
120440 KB |
Output is correct |
50 |
Correct |
563 ms |
119928 KB |
Output is correct |
51 |
Correct |
535 ms |
119892 KB |
Output is correct |
52 |
Correct |
527 ms |
117584 KB |
Output is correct |
53 |
Correct |
524 ms |
117076 KB |
Output is correct |
54 |
Correct |
530 ms |
117512 KB |
Output is correct |
55 |
Correct |
532 ms |
118880 KB |
Output is correct |