#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortvec(x) sort(all(x))
#define compress(x) x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=1987654321987654321;
const int inf=2000000000;
int n, q;
LL arr[500010], ans[500010];
pair<pii, int> query[500010];
vector<pii> poss;
int stck[500010], re;
struct SEGMENT_TREE{
int x;
struct NODE{
int st, fin;
LL f, arr, ans;
}tree[2000000];
void init(int point, int num){
if(num==1)tree[point].st=tree[point].fin=++x;
if(num<=1)return;
init(point*2, num-num/2);
init(point*2+1, num/2);
tree[point].st=tree[point*2].st;
tree[point].fin=tree[point*2+1].fin;
}
void update_arr(int point, int num, LL qu){
if(tree[point].st==tree[point].fin){
tree[point].arr=qu;
tree[point].ans=tree[point].arr+tree[point].f;
return;
}
if(num<=tree[point*2].fin)update_arr(point*2, num, qu);
else update_arr(point*2+1, num, qu);
tree[point].ans=max(max(tree[point*2].ans, tree[point*2+1].ans), tree[point*2].f+tree[point*2+1].arr);
tree[point].f=max(tree[point*2].f, tree[point*2+1].f);
tree[point].arr=max(tree[point*2].arr, tree[point*2+1].arr);
}
void update_f(int point, int num, LL qu){
if(tree[point].st==tree[point].fin){
tree[point].f=max(tree[point].f, qu);
tree[point].ans=tree[point].arr+tree[point].f;
return;
}
if(num<=tree[point*2].fin)update_f(point*2, num, qu);
else update_f(point*2+1, num, qu);
tree[point].ans=max(max(tree[point*2].ans, tree[point*2+1].ans), tree[point*2].f+tree[point*2+1].arr);
tree[point].f=max(tree[point*2].f, tree[point*2+1].f);
tree[point].arr=max(tree[point*2].arr, tree[point*2+1].arr);
}
LL query_arr(int point, int a, int b){
if(tree[point].st>=a&&tree[point].fin<=b)return tree[point].arr;
if(tree[point].st>b||tree[point].fin<a)return -llinf;
return max(query_arr(point*2, a, b), query_arr(point*2+1, a, b));
}
LL query_f(int point, int a, int b){
if(tree[point].st>=a&&tree[point].fin<=b)return tree[point].f;
if(tree[point].st>b||tree[point].fin<a)return -llinf;
return max(query_f(point*2, a, b), query_f(point*2+1, a, b));
}
LL query(int point, int a, int b){
if(tree[point].st>=a&&tree[point].fin<=b)return tree[point].ans;
if(tree[point].st>b||tree[point].fin<a)return -llinf;
return max(max(query(point*2, a, b), query(point*2+1, a, b)), query_f(point*2, a, b)+query_arr(point*2+1, a, b));
}
}seg;
int main(){
scanf("%d", &n);
seg.init(1, n);
for(int i=1; i<=n; i++){
scanf("%lld", &arr[i]);
seg.update_arr(1, i, arr[i]);
}
scanf("%d", &q);
for(int i=1; i<=q; i++){
scanf("%d %d", &query[i].F.F, &query[i].F.S);
query[i].S=i;
}
for(int i=1; i<=n; i++){
while(re&&arr[stck[re]]<=arr[i]){
poss.pb(mp(stck[re--], i));
}
if(re){
poss.pb(mp(stck[re], i));
}
stck[++re]=i;
}
sort(all(poss), greater<pii>());
sort(query+1, query+q+1, greater<pair<pii, int> >());
int pv=0;
for(int i=1; i<=q; i++){
while(pv<poss.size()&&query[i].F.F<=poss[pv].F){
if(poss[pv].S*2-poss[pv].F<=n){
seg.update_f(1, poss[pv].S*2-poss[pv].F, arr[poss[pv].F]+arr[poss[pv].S]);
//printf("inserted %d %d\n", poss[pv].F, poss[pv].S);
}
pv++;
}
//printf("answered %d %d\n", query[i].F.F, query[i].F.S);
ans[query[i].S]=seg.query(1, query[i].F.F, query[i].F.S);
}
for(int i=1; i<=q; i++)printf("%lld\n", ans[i]);
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:103:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pv<poss.size()&&query[i].F.F<=poss[pv].F){
~~^~~~~~~~~~~~
jumps.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
jumps.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &arr[i]);
~~~~~^~~~~~~~~~~~~~~~~
jumps.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
jumps.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &query[i].F.F, &query[i].F.S);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
1012 ms |
20900 KB |
Output is correct |
12 |
Correct |
1010 ms |
20472 KB |
Output is correct |
13 |
Correct |
1002 ms |
20472 KB |
Output is correct |
14 |
Correct |
1016 ms |
20856 KB |
Output is correct |
15 |
Correct |
998 ms |
20732 KB |
Output is correct |
16 |
Correct |
1000 ms |
20088 KB |
Output is correct |
17 |
Correct |
1020 ms |
19960 KB |
Output is correct |
18 |
Correct |
1022 ms |
19960 KB |
Output is correct |
19 |
Correct |
1010 ms |
20600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
24360 KB |
Output is correct |
2 |
Correct |
117 ms |
22252 KB |
Output is correct |
3 |
Correct |
123 ms |
22760 KB |
Output is correct |
4 |
Correct |
192 ms |
24292 KB |
Output is correct |
5 |
Correct |
185 ms |
24292 KB |
Output is correct |
6 |
Correct |
167 ms |
23652 KB |
Output is correct |
7 |
Correct |
168 ms |
23524 KB |
Output is correct |
8 |
Correct |
172 ms |
23396 KB |
Output is correct |
9 |
Correct |
181 ms |
23964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
1012 ms |
20900 KB |
Output is correct |
12 |
Correct |
1010 ms |
20472 KB |
Output is correct |
13 |
Correct |
1002 ms |
20472 KB |
Output is correct |
14 |
Correct |
1016 ms |
20856 KB |
Output is correct |
15 |
Correct |
998 ms |
20732 KB |
Output is correct |
16 |
Correct |
1000 ms |
20088 KB |
Output is correct |
17 |
Correct |
1020 ms |
19960 KB |
Output is correct |
18 |
Correct |
1022 ms |
19960 KB |
Output is correct |
19 |
Correct |
1010 ms |
20600 KB |
Output is correct |
20 |
Correct |
186 ms |
24360 KB |
Output is correct |
21 |
Correct |
117 ms |
22252 KB |
Output is correct |
22 |
Correct |
123 ms |
22760 KB |
Output is correct |
23 |
Correct |
192 ms |
24292 KB |
Output is correct |
24 |
Correct |
185 ms |
24292 KB |
Output is correct |
25 |
Correct |
167 ms |
23652 KB |
Output is correct |
26 |
Correct |
168 ms |
23524 KB |
Output is correct |
27 |
Correct |
172 ms |
23396 KB |
Output is correct |
28 |
Correct |
181 ms |
23964 KB |
Output is correct |
29 |
Correct |
3106 ms |
70840 KB |
Output is correct |
30 |
Correct |
2938 ms |
66788 KB |
Output is correct |
31 |
Correct |
2995 ms |
68828 KB |
Output is correct |
32 |
Correct |
3146 ms |
70864 KB |
Output is correct |
33 |
Correct |
3096 ms |
70852 KB |
Output is correct |
34 |
Correct |
3088 ms |
68824 KB |
Output is correct |
35 |
Correct |
3127 ms |
68488 KB |
Output is correct |
36 |
Correct |
3096 ms |
68296 KB |
Output is correct |
37 |
Correct |
3123 ms |
69740 KB |
Output is correct |
38 |
Correct |
2453 ms |
70896 KB |
Output is correct |
39 |
Correct |
2489 ms |
70760 KB |
Output is correct |
40 |
Correct |
2445 ms |
67764 KB |
Output is correct |
41 |
Correct |
2435 ms |
67152 KB |
Output is correct |
42 |
Correct |
2441 ms |
67040 KB |
Output is correct |
43 |
Correct |
2459 ms |
68944 KB |
Output is correct |
44 |
Correct |
2555 ms |
70960 KB |
Output is correct |
45 |
Correct |
2541 ms |
70896 KB |
Output is correct |
46 |
Correct |
2513 ms |
67744 KB |
Output is correct |
47 |
Correct |
2494 ms |
67344 KB |
Output is correct |
48 |
Correct |
2512 ms |
67616 KB |
Output is correct |
49 |
Correct |
2523 ms |
69556 KB |
Output is correct |
50 |
Correct |
2734 ms |
71212 KB |
Output is correct |
51 |
Correct |
2671 ms |
71120 KB |
Output is correct |
52 |
Correct |
2660 ms |
68584 KB |
Output is correct |
53 |
Correct |
2627 ms |
68304 KB |
Output is correct |
54 |
Correct |
2657 ms |
68096 KB |
Output is correct |
55 |
Correct |
2653 ms |
69936 KB |
Output is correct |