#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
const int N=5*1e5+50,inf=1e9;
int a[N];
int root,nc,lc[2*N],rc[2*N],maks[2*N],maks1[2*N],maksu[2*N],lazy[2*N];
void Build(int &c,int ss,int se){
c=++nc;
maks[c]=lazy[c]=-inf;
if(ss==se){
maks1[c]=a[ss];
maksu[c]=maks1[c];
return;
}
int mid=(ss+se)/2;
Build(lc[c],ss,mid);
Build(rc[c],mid+1,se);
maks1[c]=max(maks1[lc[c]],maks1[rc[c]]);
maksu[c]=maks1[c];
}
void update(int c,int qval){
lazy[c]=max(lazy[c],qval);
maks[c]=max(maks[c],qval);
maksu[c]=max(maksu[c],maks1[c]+qval);
}
void push(int c){
update(lc[c],lazy[c]);
update(rc[c],lazy[c]);
lazy[c]=-inf;
}
void Update(int c,int ss,int se,int qs,int qe,int qval){
//if(!c) c=++nc;
if(qs>qe) return;
if(qs<=ss && se<=qe){
update(c,qval);
//printf("%i %i %i: %i %i %i\n",c,ss,se,maks[c],maks1[c],maksu[c]);
return;
}
if(qe<ss || se<qs) return;
int mid=(ss+se)/2;
push(c);
Update(lc[c],ss,mid,qs,qe,qval);
Update(rc[c],mid+1,se,qs,qe,qval);
maks[c]=max(maks[lc[c]],maks[rc[c]]);
maksu[c]=max(maksu[lc[c]],maksu[rc[c]]);
//printf("%i %i %i: %i %i %i\n",c,ss,se,maks[c],maks1[c],maksu[c]);
}
int Get(int c,int ss,int se,int qs,int qe){
//printf("%i %i: %i %i %i %i\n",ss,se,maks[c],maks1[c],maksu[c],lazy[c]);
if(qs<=ss && se<=qe) {
return maksu[c];
}
if(qe<ss || se<qs) return -inf;
int mid=(ss+se)/2;
push(c);
return max(Get(lc[c],ss,mid,qs,qe),Get(rc[c],mid+1,se,qs,qe));
}
int main()
{
int n;scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&a[i]);
int Q;scanf("%i",&Q);pair<pair<int,int>,int>Qs[Q+10];
for(int i=1;i<=Q;i++) {scanf("%i%i",&Qs[i].fi.fi,&Qs[i].fi.se);Qs[i].se=i;}
vector<int>mono;
vector<int>R[n+10];
for(int i=1;i<=n;i++){
while(mono.size() && a[mono.back()]<a[i]){
R[mono.back()].pb(i);
mono.pop_back();
}
//if(mono.size() && a[mono.back()]==a[i]){R[mono.back()].pb(i);mono.pop_back();}
mono.pb(i);
}
mono.clear();
for(int i=1;i<=n;i++){
while(mono.size() && a[mono.back()]<a[i]){
mono.pop_back();
//R[mono.back()].pb(i);
//int temp=mono.back();
//while(mono.size() && a[mono.back()]==a[temp]) mono.pop_back();
}
if(mono.size()) R[mono.back()].pb(i);
mono.pb(i);
//for(auto j:mono) printf("%i ",j);
//printf("\n");
}
//printf("*\n");
//for(int i=1;i<=n;i++) for(auto j:R[i]) printf("%i %i\n",i,j);
/*vector<int>R[n+10];
for(int i=1;i<=n;i++){
//printf("%i: ",i);
//int mx=a[i];
for(int j=i+1;j<=n;j++){
//mx=max(mx,a[j]);
int mx=0;
for(int k=i+1;k<j;k++){
mx=max(mx,a[k]);
}
if(mx<a[i] && mx<a[j]) R[i].pb(j);//printf("%i ",R[i].back());}
}
//printf("\n");
}*/
Build(root,1,n);
int res[Q+10]={0};
sort(Qs+1,Qs+Q+1);
for(int i=n,j=Q;i>=1;i--){
for(auto j:R[i]){//if(R[i]!=0){
//a=i,b=R[i], b-a<=c-b, 2b-a=2R[i]-i<=c
if(2*j-i<=n) Update(root,1,n,2*j-i,n,a[i]+a[j]);//printf("%i %i %i %i: %i\n",i,j,2*j-i,n,a[i]+a[j]);}
}
while(j>=1 && Qs[j].fi.fi==i){
res[Qs[j].se]=Get(root,1,n,Qs[j].fi.fi,Qs[j].fi.se);
//printf("%i %i %i %i\n",res[Qs[j].se],Qs[j].fi.fi,Qs[j].fi.se,Qs[j].se);
j--;
}
//if(i==3) printf("- %i -\n",Get(root,1,n,3,5));
}
for(int i=1;i<=Q;i++) printf("%i ",res[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:63:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | int n;scanf("%i",&n);
| ~~~~~^~~~~~~~~
jumps.cpp:64:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | for(int i=1;i<=n;i++)scanf("%i",&a[i]);
| ~~~~~^~~~~~~~~~~~
jumps.cpp:65:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | int Q;scanf("%i",&Q);pair<pair<int,int>,int>Qs[Q+10];
| ~~~~~^~~~~~~~~
jumps.cpp:66:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | for(int i=1;i<=Q;i++) {scanf("%i%i",&Qs[i].fi.fi,&Qs[i].fi.se);Qs[i].se=i;}
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12760 KB |
Output is correct |
11 |
Correct |
299 ms |
25752 KB |
Output is correct |
12 |
Correct |
287 ms |
30456 KB |
Output is correct |
13 |
Correct |
331 ms |
30860 KB |
Output is correct |
14 |
Correct |
291 ms |
30544 KB |
Output is correct |
15 |
Correct |
306 ms |
30472 KB |
Output is correct |
16 |
Correct |
299 ms |
29776 KB |
Output is correct |
17 |
Correct |
296 ms |
29780 KB |
Output is correct |
18 |
Correct |
296 ms |
29720 KB |
Output is correct |
19 |
Correct |
298 ms |
30360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
36712 KB |
Output is correct |
2 |
Correct |
84 ms |
37916 KB |
Output is correct |
3 |
Correct |
85 ms |
38720 KB |
Output is correct |
4 |
Correct |
147 ms |
38464 KB |
Output is correct |
5 |
Correct |
146 ms |
38484 KB |
Output is correct |
6 |
Correct |
144 ms |
37796 KB |
Output is correct |
7 |
Correct |
146 ms |
37696 KB |
Output is correct |
8 |
Correct |
143 ms |
37700 KB |
Output is correct |
9 |
Correct |
147 ms |
38036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12760 KB |
Output is correct |
11 |
Correct |
299 ms |
25752 KB |
Output is correct |
12 |
Correct |
287 ms |
30456 KB |
Output is correct |
13 |
Correct |
331 ms |
30860 KB |
Output is correct |
14 |
Correct |
291 ms |
30544 KB |
Output is correct |
15 |
Correct |
306 ms |
30472 KB |
Output is correct |
16 |
Correct |
299 ms |
29776 KB |
Output is correct |
17 |
Correct |
296 ms |
29780 KB |
Output is correct |
18 |
Correct |
296 ms |
29720 KB |
Output is correct |
19 |
Correct |
298 ms |
30360 KB |
Output is correct |
20 |
Correct |
150 ms |
36712 KB |
Output is correct |
21 |
Correct |
84 ms |
37916 KB |
Output is correct |
22 |
Correct |
85 ms |
38720 KB |
Output is correct |
23 |
Correct |
147 ms |
38464 KB |
Output is correct |
24 |
Correct |
146 ms |
38484 KB |
Output is correct |
25 |
Correct |
144 ms |
37796 KB |
Output is correct |
26 |
Correct |
146 ms |
37696 KB |
Output is correct |
27 |
Correct |
143 ms |
37700 KB |
Output is correct |
28 |
Correct |
147 ms |
38036 KB |
Output is correct |
29 |
Correct |
951 ms |
78420 KB |
Output is correct |
30 |
Correct |
774 ms |
76928 KB |
Output is correct |
31 |
Correct |
814 ms |
79036 KB |
Output is correct |
32 |
Correct |
951 ms |
78284 KB |
Output is correct |
33 |
Correct |
972 ms |
78580 KB |
Output is correct |
34 |
Correct |
1045 ms |
76144 KB |
Output is correct |
35 |
Correct |
975 ms |
75632 KB |
Output is correct |
36 |
Correct |
922 ms |
75740 KB |
Output is correct |
37 |
Correct |
941 ms |
77284 KB |
Output is correct |
38 |
Correct |
739 ms |
78504 KB |
Output is correct |
39 |
Correct |
735 ms |
78416 KB |
Output is correct |
40 |
Correct |
704 ms |
74892 KB |
Output is correct |
41 |
Correct |
731 ms |
74600 KB |
Output is correct |
42 |
Correct |
716 ms |
74404 KB |
Output is correct |
43 |
Correct |
729 ms |
76332 KB |
Output is correct |
44 |
Correct |
754 ms |
78376 KB |
Output is correct |
45 |
Correct |
735 ms |
78352 KB |
Output is correct |
46 |
Correct |
733 ms |
75152 KB |
Output is correct |
47 |
Correct |
763 ms |
74880 KB |
Output is correct |
48 |
Correct |
791 ms |
74876 KB |
Output is correct |
49 |
Correct |
718 ms |
76856 KB |
Output is correct |
50 |
Correct |
767 ms |
78436 KB |
Output is correct |
51 |
Correct |
805 ms |
78416 KB |
Output is correct |
52 |
Correct |
769 ms |
75964 KB |
Output is correct |
53 |
Correct |
805 ms |
75604 KB |
Output is correct |
54 |
Correct |
756 ms |
75692 KB |
Output is correct |
55 |
Correct |
771 ms |
77328 KB |
Output is correct |