#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ff first
#define ss second
int A[500005], N, Q, stk[500005], stn;
const int SIZE = 1 << 19;
struct ST {
struct Node {
unsigned a, b, v;
} X[2 * SIZE];
void init(int nn, int s, int e) {
if (s == e) {
X[nn].a = A[s];
return;
}
int m = s + e >> 1;
init(nn << 1, s, m);
init(nn << 1 | 1, m + 1, e);
X[nn].a = max(X[nn << 1].a, X[nn << 1 | 1].a);
}
void busy(int nn) {
if (X[nn << 1].b < X[nn].b) {
X[nn << 1].b = X[nn].b;
X[nn << 1].v = max(X[nn << 1].v, X[nn << 1].a + X[nn].b);
}
if (X[nn << 1 | 1].b < X[nn].b) {
X[nn << 1 | 1].b = X[nn].b;
X[nn << 1 | 1].v = max(X[nn << 1 | 1].v, X[nn << 1 | 1].a + X[nn].b);
}
}
void update(int nn, int ns, int ne, int s, int e, unsigned v) {
if (ns > e || ne < s) return;
if (s <= ns && ne <= e) {
if (X[nn].b < v) {
X[nn].b = v;
X[nn].v = max(X[nn].v, X[nn].a + v);
}
return;
}
int m = ns + ne >> 1;
busy(nn);
update(nn << 1, ns, m, s, e, v);
update(nn << 1 | 1, m + 1, ne, s, e, v);
X[nn].v = max(X[nn << 1].v, X[nn << 1 | 1].v);
}
unsigned query(int nn, int ns, int ne, int s, int e) {
if (ns > e || ne < s) return 0;
if (s <= ns && ne <= e) return X[nn].v;
int m = ns + ne >> 1;
busy(nn);
return max(query(nn << 1, ns, m, s, e), query(nn << 1 | 1, m + 1, ne, s, e));
}
} seg;
vector<pair<int, int>> qry[500005];
vector<int> can[500005];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N;
for (int i = 1; i <= N; ++i) cin >> A[i];
seg.init(1, 1, N);
for (int i = 1; i <= N; ++i) {
while (stn && A[stk[stn - 1]] < A[i]) {
--stn;
can[stk[stn]].push_back(i);
}
if (stn) can[stk[stn - 1]].push_back(i);
if (stn && A[i] == A[stk[stn - 1]]) stn--;
stk[stn++] = i;
}
cin >> Q;
vector<unsigned> ans(Q);
for (int i = 0; i < Q; ++i) {
int x, y; cin >> x >> y;
qry[x].emplace_back(y, i);
}
for (int i = N; i; --i) {
for (auto j : can[i]) seg.update(1, 1, N, j + j - i, N, A[i] + A[j]);
for (auto j : qry[i]) ans[j.ss] = seg.query(1, 1, N, i, j.ff);
}
for (auto i : ans) printf("%u\n", i);
return 0;
}
Compilation message
jumps.cpp: In member function 'void ST::init(int, int, int)':
jumps.cpp:18:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s + e >> 1;
~~^~~
jumps.cpp: In member function 'void ST::update(int, int, int, int, int, unsigned int)':
jumps.cpp:42:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = ns + ne >> 1;
~~~^~~~
jumps.cpp: In member function 'unsigned int ST::query(int, int, int, int, int)':
jumps.cpp:51:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = ns + ne >> 1;
~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23928 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
19 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23928 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
19 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
11 |
Correct |
291 ms |
42292 KB |
Output is correct |
12 |
Correct |
290 ms |
42232 KB |
Output is correct |
13 |
Correct |
354 ms |
42360 KB |
Output is correct |
14 |
Correct |
299 ms |
42232 KB |
Output is correct |
15 |
Correct |
292 ms |
42360 KB |
Output is correct |
16 |
Correct |
301 ms |
41720 KB |
Output is correct |
17 |
Correct |
305 ms |
41720 KB |
Output is correct |
18 |
Correct |
295 ms |
41592 KB |
Output is correct |
19 |
Correct |
299 ms |
42120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
37524 KB |
Output is correct |
2 |
Correct |
89 ms |
36984 KB |
Output is correct |
3 |
Correct |
96 ms |
37752 KB |
Output is correct |
4 |
Correct |
144 ms |
37240 KB |
Output is correct |
5 |
Correct |
164 ms |
37368 KB |
Output is correct |
6 |
Correct |
134 ms |
37240 KB |
Output is correct |
7 |
Correct |
133 ms |
37244 KB |
Output is correct |
8 |
Correct |
131 ms |
37240 KB |
Output is correct |
9 |
Correct |
141 ms |
37368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23928 KB |
Output is correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23928 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
19 ms |
23800 KB |
Output is correct |
8 |
Correct |
19 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
18 ms |
23800 KB |
Output is correct |
11 |
Correct |
291 ms |
42292 KB |
Output is correct |
12 |
Correct |
290 ms |
42232 KB |
Output is correct |
13 |
Correct |
354 ms |
42360 KB |
Output is correct |
14 |
Correct |
299 ms |
42232 KB |
Output is correct |
15 |
Correct |
292 ms |
42360 KB |
Output is correct |
16 |
Correct |
301 ms |
41720 KB |
Output is correct |
17 |
Correct |
305 ms |
41720 KB |
Output is correct |
18 |
Correct |
295 ms |
41592 KB |
Output is correct |
19 |
Correct |
299 ms |
42120 KB |
Output is correct |
20 |
Correct |
141 ms |
37524 KB |
Output is correct |
21 |
Correct |
89 ms |
36984 KB |
Output is correct |
22 |
Correct |
96 ms |
37752 KB |
Output is correct |
23 |
Correct |
144 ms |
37240 KB |
Output is correct |
24 |
Correct |
164 ms |
37368 KB |
Output is correct |
25 |
Correct |
134 ms |
37240 KB |
Output is correct |
26 |
Correct |
133 ms |
37244 KB |
Output is correct |
27 |
Correct |
131 ms |
37240 KB |
Output is correct |
28 |
Correct |
141 ms |
37368 KB |
Output is correct |
29 |
Correct |
948 ms |
82168 KB |
Output is correct |
30 |
Correct |
842 ms |
81656 KB |
Output is correct |
31 |
Correct |
850 ms |
83628 KB |
Output is correct |
32 |
Correct |
972 ms |
82168 KB |
Output is correct |
33 |
Correct |
993 ms |
82300 KB |
Output is correct |
34 |
Correct |
946 ms |
79992 KB |
Output is correct |
35 |
Correct |
932 ms |
79608 KB |
Output is correct |
36 |
Correct |
962 ms |
79608 KB |
Output is correct |
37 |
Correct |
959 ms |
81224 KB |
Output is correct |
38 |
Correct |
697 ms |
87928 KB |
Output is correct |
39 |
Correct |
682 ms |
88056 KB |
Output is correct |
40 |
Correct |
657 ms |
84728 KB |
Output is correct |
41 |
Correct |
680 ms |
84276 KB |
Output is correct |
42 |
Correct |
670 ms |
84132 KB |
Output is correct |
43 |
Correct |
673 ms |
85752 KB |
Output is correct |
44 |
Correct |
735 ms |
87384 KB |
Output is correct |
45 |
Correct |
724 ms |
87288 KB |
Output is correct |
46 |
Correct |
678 ms |
84220 KB |
Output is correct |
47 |
Correct |
690 ms |
83832 KB |
Output is correct |
48 |
Correct |
679 ms |
83832 KB |
Output is correct |
49 |
Correct |
669 ms |
85752 KB |
Output is correct |
50 |
Correct |
783 ms |
85572 KB |
Output is correct |
51 |
Correct |
774 ms |
85496 KB |
Output is correct |
52 |
Correct |
761 ms |
83028 KB |
Output is correct |
53 |
Correct |
775 ms |
82808 KB |
Output is correct |
54 |
Correct |
766 ms |
82760 KB |
Output is correct |
55 |
Correct |
768 ms |
84188 KB |
Output is correct |