#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Query {
int l, r, idx;
Query(){}
Query(int l, int r, int idx): l(l), r(r), idx(idx){}
};
struct segTree{
struct Node{
int l, r, v;
Node(){}
Node(int l, int r, int v): l(l), r(r), v(v){}
Node operator+(const Node &nxt)const{
return Node(max(l, nxt.l), max(r, nxt.r), max({v, nxt.v, l+nxt.r}));
}
} tree[2000002];
void init(int i, int l, int r, int *arr){
if(l==r){
tree[i] = Node(-1e9, arr[l], -1e9);
return;
}
int m = (l+r)>>1;
init(i*2, l, m, arr);
init(i*2+1, m+1, r, arr);
tree[i] = tree[i*2] + tree[i*2+1];
}
Node query(int i, int l, int r, int s, int e){
if(r<s || e<l) return Node(-1e9, -1e9, -1e9);
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return query(i*2, l, m, s, e) + query(i*2+1, m+1, r, s, e);
}
void update(int i, int l, int r, int x, int v){
if(l==r){
tree[i].l = max(tree[i].l, v);
tree[i].v = max(tree[i].v, tree[i].l+tree[i].r);
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, v);
else update(i*2+1, m+1, r, x, v);
tree[i] = tree[i*2] + tree[i*2+1];
}
} tree;
int n, q;
int arr[500002];
vector<int> interval[500002];
vector<Query> query[500002];
int ans[500002];
void makeInterval(){
vector<pair<int, int> > vec;
for(int i=1; i<=n; i++){
while(!vec.empty() && vec.back().second <= arr[i]){
interval[vec.back().first].push_back(i);
vec.pop_back();
}
if(!vec.empty()) interval[vec.back().first].push_back(i);
vec.push_back(make_pair(i, arr[i]));
}
}
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
makeInterval();
scanf("%d", &q);
for(int i=1; i<=q; i++){
int l, r;
scanf("%d %d", &l, &r);
query[l].push_back(Query(l, r, i));
}
tree.init(1, 1, n, arr);
for(int i=n-2; i>=1; i--){
for(int j: interval[i]){
if(2*j-i>n) continue;
tree.update(1, 1, n, 2*j-i, arr[i]+arr[j]);
}
for(Query p: query[i]){
ans[p.idx] = tree.query(1, 1, n, i, p.r).v;
}
}
for(int i=1; i<=q; i++){
printf("%d\n", ans[i]);
}
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
jumps.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
jumps.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
jumps.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23796 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
11 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23796 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
11 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
11 |
Correct |
275 ms |
43872 KB |
Output is correct |
12 |
Correct |
279 ms |
43808 KB |
Output is correct |
13 |
Correct |
284 ms |
43792 KB |
Output is correct |
14 |
Correct |
261 ms |
43948 KB |
Output is correct |
15 |
Correct |
264 ms |
43972 KB |
Output is correct |
16 |
Correct |
289 ms |
43252 KB |
Output is correct |
17 |
Correct |
279 ms |
43276 KB |
Output is correct |
18 |
Correct |
281 ms |
43156 KB |
Output is correct |
19 |
Correct |
272 ms |
43804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
38856 KB |
Output is correct |
2 |
Correct |
75 ms |
38656 KB |
Output is correct |
3 |
Correct |
70 ms |
38724 KB |
Output is correct |
4 |
Correct |
111 ms |
38940 KB |
Output is correct |
5 |
Correct |
124 ms |
38944 KB |
Output is correct |
6 |
Correct |
106 ms |
38340 KB |
Output is correct |
7 |
Correct |
107 ms |
38196 KB |
Output is correct |
8 |
Correct |
114 ms |
38164 KB |
Output is correct |
9 |
Correct |
116 ms |
38508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23796 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
11 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23796 KB |
Output is correct |
11 |
Correct |
275 ms |
43872 KB |
Output is correct |
12 |
Correct |
279 ms |
43808 KB |
Output is correct |
13 |
Correct |
284 ms |
43792 KB |
Output is correct |
14 |
Correct |
261 ms |
43948 KB |
Output is correct |
15 |
Correct |
264 ms |
43972 KB |
Output is correct |
16 |
Correct |
289 ms |
43252 KB |
Output is correct |
17 |
Correct |
279 ms |
43276 KB |
Output is correct |
18 |
Correct |
281 ms |
43156 KB |
Output is correct |
19 |
Correct |
272 ms |
43804 KB |
Output is correct |
20 |
Correct |
111 ms |
38856 KB |
Output is correct |
21 |
Correct |
75 ms |
38656 KB |
Output is correct |
22 |
Correct |
70 ms |
38724 KB |
Output is correct |
23 |
Correct |
111 ms |
38940 KB |
Output is correct |
24 |
Correct |
124 ms |
38944 KB |
Output is correct |
25 |
Correct |
106 ms |
38340 KB |
Output is correct |
26 |
Correct |
107 ms |
38196 KB |
Output is correct |
27 |
Correct |
114 ms |
38164 KB |
Output is correct |
28 |
Correct |
116 ms |
38508 KB |
Output is correct |
29 |
Correct |
803 ms |
75424 KB |
Output is correct |
30 |
Correct |
671 ms |
74696 KB |
Output is correct |
31 |
Correct |
681 ms |
74700 KB |
Output is correct |
32 |
Correct |
811 ms |
75372 KB |
Output is correct |
33 |
Correct |
798 ms |
75380 KB |
Output is correct |
34 |
Correct |
836 ms |
74756 KB |
Output is correct |
35 |
Correct |
826 ms |
74620 KB |
Output is correct |
36 |
Correct |
782 ms |
74736 KB |
Output is correct |
37 |
Correct |
792 ms |
75260 KB |
Output is correct |
38 |
Correct |
570 ms |
80032 KB |
Output is correct |
39 |
Correct |
574 ms |
80104 KB |
Output is correct |
40 |
Correct |
556 ms |
78340 KB |
Output is correct |
41 |
Correct |
577 ms |
78104 KB |
Output is correct |
42 |
Correct |
565 ms |
78188 KB |
Output is correct |
43 |
Correct |
572 ms |
79012 KB |
Output is correct |
44 |
Correct |
602 ms |
79504 KB |
Output is correct |
45 |
Correct |
593 ms |
79500 KB |
Output is correct |
46 |
Correct |
586 ms |
78004 KB |
Output is correct |
47 |
Correct |
611 ms |
77768 KB |
Output is correct |
48 |
Correct |
655 ms |
77796 KB |
Output is correct |
49 |
Correct |
598 ms |
79044 KB |
Output is correct |
50 |
Correct |
654 ms |
77712 KB |
Output is correct |
51 |
Correct |
679 ms |
77692 KB |
Output is correct |
52 |
Correct |
668 ms |
76816 KB |
Output is correct |
53 |
Correct |
677 ms |
76976 KB |
Output is correct |
54 |
Correct |
632 ms |
76856 KB |
Output is correct |
55 |
Correct |
693 ms |
77660 KB |
Output is correct |