#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct node { int v, f, a; } T[1050000];
const node EMP = {0, 0, 0};
node operator +(node L, node R) {
node ret;
ret.f = max(L.f, R.f);
ret.a = max(L.a, R.a);
ret.v = max({L.v, R.v, L.f + R.a});
return ret;
}
int A[500009], ans[500009], F[500009];
vector<pii> query[500009];
vector<int> add[500009];
void init(int idx, int s, int e) {
if(s == e) {
T[idx] = {A[s], 0, A[s]};
return;
}
int m = s+e >> 1;
init(idx*2, s, m);
init(idx*2+1, m+1, e);
T[idx] = T[idx*2] + T[idx*2+1];
}
void upd(int idx, int s, int e, int p, int v) {
if(p < s || e < p) return;
if(s == e) {
T[idx].f = v;
T[idx].v = T[idx].f + T[idx].a;
return;
}
int m = s+e >> 1;
upd(idx*2, s, m, p, v);
upd(idx*2+1, m+1, e, p, v);
T[idx] = T[idx*2] + T[idx*2+1];
}
node get(int idx, int s, int e, int l, int r) {
if(r < s || e < l) return EMP;
if(l <= s && e <= r) return T[idx];
int m = s+e >> 1;
return get(idx*2, s, m, l, r) + get(idx*2+1, m+1, e, l, r);
}
int main() {
int N; scanf("%d", &N);
for(int i=1; i<=N; i++) scanf("%d", &A[i]);
int Q; scanf("%d", &Q);
for(int i=0; i<Q; i++) {
int l, r; scanf("%d%d",&l,&r);
query[l].push_back({r, i});
}
vector<int> S;
for(int i=1; i<=N; i++) {
while(S.size() && A[S.back()] < A[i]) {
add[S.back()].push_back(i);
S.pop_back();
}
if(S.size()) add[S.back()].push_back(i);
S.push_back(i);
}
init(1, 1, N);
for(int i=N; i>=1; i--) {
for(auto& it: add[i]) if(2*it - i <= N) {
F[2*it - i] = max(F[2*it - i], A[i] + A[it]);
upd(1, 1, N, 2*it - i, F[2*it - i]);
}
for(auto& it: query[i]) ans[it.second] = get(1, 1, N, i, it.first).v;
}
for(int i=0; i<Q; i++) printf("%d\n", ans[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'void init(int, int, int)':
jumps.cpp:24:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
jumps.cpp: In function 'void upd(int, int, int, int, int)':
jumps.cpp:37:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
jumps.cpp: In function 'node get(int, int, int, int, int)':
jumps.cpp:46:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
jumps.cpp: In function 'int main()':
jumps.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int N; scanf("%d", &N);
~~~~~^~~~~~~~~~
jumps.cpp:52:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
jumps.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int Q; scanf("%d", &Q);
~~~~~^~~~~~~~~~
jumps.cpp:55:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int l, r; scanf("%d%d",&l,&r);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
452 ms |
42100 KB |
Output is correct |
12 |
Correct |
442 ms |
42104 KB |
Output is correct |
13 |
Correct |
445 ms |
42276 KB |
Output is correct |
14 |
Correct |
442 ms |
42108 KB |
Output is correct |
15 |
Correct |
443 ms |
42232 KB |
Output is correct |
16 |
Correct |
450 ms |
41592 KB |
Output is correct |
17 |
Correct |
437 ms |
41464 KB |
Output is correct |
18 |
Correct |
452 ms |
41360 KB |
Output is correct |
19 |
Correct |
441 ms |
42232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
232 ms |
39800 KB |
Output is correct |
2 |
Correct |
143 ms |
39544 KB |
Output is correct |
3 |
Correct |
140 ms |
40284 KB |
Output is correct |
4 |
Correct |
233 ms |
39800 KB |
Output is correct |
5 |
Correct |
236 ms |
39720 KB |
Output is correct |
6 |
Correct |
220 ms |
39160 KB |
Output is correct |
7 |
Correct |
221 ms |
39032 KB |
Output is correct |
8 |
Correct |
220 ms |
39080 KB |
Output is correct |
9 |
Correct |
228 ms |
39408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
452 ms |
42100 KB |
Output is correct |
12 |
Correct |
442 ms |
42104 KB |
Output is correct |
13 |
Correct |
445 ms |
42276 KB |
Output is correct |
14 |
Correct |
442 ms |
42108 KB |
Output is correct |
15 |
Correct |
443 ms |
42232 KB |
Output is correct |
16 |
Correct |
450 ms |
41592 KB |
Output is correct |
17 |
Correct |
437 ms |
41464 KB |
Output is correct |
18 |
Correct |
452 ms |
41360 KB |
Output is correct |
19 |
Correct |
441 ms |
42232 KB |
Output is correct |
20 |
Correct |
232 ms |
39800 KB |
Output is correct |
21 |
Correct |
143 ms |
39544 KB |
Output is correct |
22 |
Correct |
140 ms |
40284 KB |
Output is correct |
23 |
Correct |
233 ms |
39800 KB |
Output is correct |
24 |
Correct |
236 ms |
39720 KB |
Output is correct |
25 |
Correct |
220 ms |
39160 KB |
Output is correct |
26 |
Correct |
221 ms |
39032 KB |
Output is correct |
27 |
Correct |
220 ms |
39080 KB |
Output is correct |
28 |
Correct |
228 ms |
39408 KB |
Output is correct |
29 |
Correct |
1427 ms |
84188 KB |
Output is correct |
30 |
Correct |
1169 ms |
83704 KB |
Output is correct |
31 |
Correct |
1174 ms |
85604 KB |
Output is correct |
32 |
Correct |
1432 ms |
84024 KB |
Output is correct |
33 |
Correct |
1396 ms |
84232 KB |
Output is correct |
34 |
Correct |
1392 ms |
81860 KB |
Output is correct |
35 |
Correct |
1389 ms |
81728 KB |
Output is correct |
36 |
Correct |
1390 ms |
81784 KB |
Output is correct |
37 |
Correct |
1385 ms |
83192 KB |
Output is correct |
38 |
Correct |
1096 ms |
89960 KB |
Output is correct |
39 |
Correct |
1085 ms |
89864 KB |
Output is correct |
40 |
Correct |
1061 ms |
86648 KB |
Output is correct |
41 |
Correct |
1040 ms |
86008 KB |
Output is correct |
42 |
Correct |
1068 ms |
86012 KB |
Output is correct |
43 |
Correct |
1068 ms |
87672 KB |
Output is correct |
44 |
Correct |
1128 ms |
89336 KB |
Output is correct |
45 |
Correct |
1123 ms |
89208 KB |
Output is correct |
46 |
Correct |
1101 ms |
86264 KB |
Output is correct |
47 |
Correct |
1091 ms |
85740 KB |
Output is correct |
48 |
Correct |
1106 ms |
85660 KB |
Output is correct |
49 |
Correct |
1112 ms |
87672 KB |
Output is correct |
50 |
Correct |
1207 ms |
87032 KB |
Output is correct |
51 |
Correct |
1201 ms |
87416 KB |
Output is correct |
52 |
Correct |
1191 ms |
84856 KB |
Output is correct |
53 |
Correct |
1157 ms |
84260 KB |
Output is correct |
54 |
Correct |
1177 ms |
84348 KB |
Output is correct |
55 |
Correct |
1199 ms |
85928 KB |
Output is correct |