#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=5e5+99,xn=N<<2,inf=1e10;
int n,m,a[N],ans[N],seg[xn],lazy[xn],Mx[xn];
int L[N],R[N];
vector<int> vec[N];
vector<pair<int,int>> Q[N];
void shift(int ,int ,int );
void upd(int id){
Mx[id]=max(Mx[id<<1],Mx[id<<1|1]);
seg[id]=max(seg[id<<1],seg[id<<1|1]);
}
void find(){
a[0]=inf;
a[n+1]=inf;
f(i,1,n+1){
L[i]=i-1;
while(a[L[i]]<a[i]){
L[i]=L[L[i]];
}
if(0<L[i]) vec[L[i]].pb(i);
}
f_(i,n,1){
R[i]=i+1;
while(a[R[i]]<a[i]){
R[i]=R[R[i]];
}
if(R[i]<=n) vec[i].pb(R[i]);
}
}
void build(int id=1,int l=1,int r=n+1){
if(l+1==r){
Mx[id]=a[l];
return ;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid,r);
upd(id);
}
void update(int L,int R,int val,int id=1,int l=1,int r=n+1){
if(r<=L || R<=l) return ;
if(L<=l && r<=R){
maxm(lazy[id],val);
maxm(seg[id],Mx[id]+val);
//eror(seg[id]);
//cout<<l<<" ::: "<<r<<endl;
return ;
}
int mid=(l+r)>>1;
shift(id,l,r);
update(L,R,val,id<<1,l,mid);
update(L,R,val,id<<1|1,mid,r);
upd(id);
}
int query(int L,int R,int id=1,int l=1,int r=n+1){
//cout<<L<<" "<<R<<" "<<l<<" "<<R<<endl;
if(r<=L || R<=l) return -inf;
if(L<=l && r<=R){
//cout<<l<<" ----> "<<r<<endl;
return seg[id];
}
int mid=(l+r)>>1;
shift(id,l,r);
return max(query(L,R,id<<1,l,mid),query(L,R,id<<1|1,mid,r));
}
void solve(){
f_(i,n,1){
for(auto x : vec[i]){
if(2*x-i>n) continue ;
//eror(a[i]+a[x]);
update(2*x-i,n+1,a[i]+a[x]);
}
for(auto x : Q[i]){
//cout<<x.S<<" -> "<<query(i,x.F+1)<<endl;
ans[x.S]=query(i,x.F+1);
}
}
}
void shift(int id,int l,int r){
int mid=(l+r)>>1;
maxm(lazy[id<<1],lazy[id]);
maxm(lazy[id<<1|1],lazy[id]);
maxm(seg[id<<1],Mx[id<<1]+lazy[id]);
maxm(seg[id<<1|1],Mx[id<<1|1]+lazy[id]);
}
main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
fill(lazy,lazy+xn,-inf);
fill(Mx,Mx+xn,-inf);
cin>>n;
f(i,1,n+1) cin>>a[i];
cin>>m;
f(i,1,m+1){
int l,r;
cin>>l>>r;
Q[l].pb({r,i});
}
find();
build();
solve();
f(i,1,m+1) cout<<ans[i]<<" ";
}
/*
5
5 2 1 5 3
1
2 5
1 5
*/
Compilation message
jumps.cpp: In function 'void shift(long long int, long long int, long long int)':
jumps.cpp:105:6: warning: unused variable 'mid' [-Wunused-variable]
105 | int mid=(l+r)>>1;
| ^~~
jumps.cpp: At global scope:
jumps.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
112 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55132 KB |
Output is correct |
3 |
Correct |
25 ms |
55124 KB |
Output is correct |
4 |
Correct |
27 ms |
55060 KB |
Output is correct |
5 |
Correct |
25 ms |
55112 KB |
Output is correct |
6 |
Correct |
24 ms |
55052 KB |
Output is correct |
7 |
Correct |
28 ms |
55064 KB |
Output is correct |
8 |
Correct |
25 ms |
55124 KB |
Output is correct |
9 |
Correct |
25 ms |
55108 KB |
Output is correct |
10 |
Correct |
25 ms |
55164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55132 KB |
Output is correct |
3 |
Correct |
25 ms |
55124 KB |
Output is correct |
4 |
Correct |
27 ms |
55060 KB |
Output is correct |
5 |
Correct |
25 ms |
55112 KB |
Output is correct |
6 |
Correct |
24 ms |
55052 KB |
Output is correct |
7 |
Correct |
28 ms |
55064 KB |
Output is correct |
8 |
Correct |
25 ms |
55124 KB |
Output is correct |
9 |
Correct |
25 ms |
55108 KB |
Output is correct |
10 |
Correct |
25 ms |
55164 KB |
Output is correct |
11 |
Correct |
261 ms |
82312 KB |
Output is correct |
12 |
Correct |
261 ms |
82384 KB |
Output is correct |
13 |
Correct |
268 ms |
82492 KB |
Output is correct |
14 |
Correct |
274 ms |
82288 KB |
Output is correct |
15 |
Correct |
261 ms |
82376 KB |
Output is correct |
16 |
Correct |
270 ms |
81760 KB |
Output is correct |
17 |
Correct |
269 ms |
81816 KB |
Output is correct |
18 |
Correct |
272 ms |
81688 KB |
Output is correct |
19 |
Correct |
270 ms |
82316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
73156 KB |
Output is correct |
2 |
Correct |
91 ms |
71888 KB |
Output is correct |
3 |
Correct |
103 ms |
72012 KB |
Output is correct |
4 |
Correct |
144 ms |
73048 KB |
Output is correct |
5 |
Correct |
144 ms |
73156 KB |
Output is correct |
6 |
Correct |
139 ms |
72516 KB |
Output is correct |
7 |
Correct |
140 ms |
72376 KB |
Output is correct |
8 |
Correct |
141 ms |
72284 KB |
Output is correct |
9 |
Correct |
142 ms |
72620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55132 KB |
Output is correct |
3 |
Correct |
25 ms |
55124 KB |
Output is correct |
4 |
Correct |
27 ms |
55060 KB |
Output is correct |
5 |
Correct |
25 ms |
55112 KB |
Output is correct |
6 |
Correct |
24 ms |
55052 KB |
Output is correct |
7 |
Correct |
28 ms |
55064 KB |
Output is correct |
8 |
Correct |
25 ms |
55124 KB |
Output is correct |
9 |
Correct |
25 ms |
55108 KB |
Output is correct |
10 |
Correct |
25 ms |
55164 KB |
Output is correct |
11 |
Correct |
261 ms |
82312 KB |
Output is correct |
12 |
Correct |
261 ms |
82384 KB |
Output is correct |
13 |
Correct |
268 ms |
82492 KB |
Output is correct |
14 |
Correct |
274 ms |
82288 KB |
Output is correct |
15 |
Correct |
261 ms |
82376 KB |
Output is correct |
16 |
Correct |
270 ms |
81760 KB |
Output is correct |
17 |
Correct |
269 ms |
81816 KB |
Output is correct |
18 |
Correct |
272 ms |
81688 KB |
Output is correct |
19 |
Correct |
270 ms |
82316 KB |
Output is correct |
20 |
Correct |
145 ms |
73156 KB |
Output is correct |
21 |
Correct |
91 ms |
71888 KB |
Output is correct |
22 |
Correct |
103 ms |
72012 KB |
Output is correct |
23 |
Correct |
144 ms |
73048 KB |
Output is correct |
24 |
Correct |
144 ms |
73156 KB |
Output is correct |
25 |
Correct |
139 ms |
72516 KB |
Output is correct |
26 |
Correct |
140 ms |
72376 KB |
Output is correct |
27 |
Correct |
141 ms |
72284 KB |
Output is correct |
28 |
Correct |
142 ms |
72620 KB |
Output is correct |
29 |
Correct |
915 ms |
126768 KB |
Output is correct |
30 |
Correct |
756 ms |
123716 KB |
Output is correct |
31 |
Correct |
797 ms |
123676 KB |
Output is correct |
32 |
Correct |
929 ms |
126848 KB |
Output is correct |
33 |
Correct |
897 ms |
126636 KB |
Output is correct |
34 |
Correct |
888 ms |
124444 KB |
Output is correct |
35 |
Correct |
891 ms |
124224 KB |
Output is correct |
36 |
Correct |
889 ms |
124164 KB |
Output is correct |
37 |
Correct |
893 ms |
125560 KB |
Output is correct |
38 |
Correct |
624 ms |
129264 KB |
Output is correct |
39 |
Correct |
627 ms |
129208 KB |
Output is correct |
40 |
Correct |
609 ms |
125876 KB |
Output is correct |
41 |
Correct |
606 ms |
125360 KB |
Output is correct |
42 |
Correct |
607 ms |
125380 KB |
Output is correct |
43 |
Correct |
650 ms |
127268 KB |
Output is correct |
44 |
Correct |
670 ms |
129100 KB |
Output is correct |
45 |
Correct |
681 ms |
129144 KB |
Output is correct |
46 |
Correct |
661 ms |
125864 KB |
Output is correct |
47 |
Correct |
643 ms |
125440 KB |
Output is correct |
48 |
Correct |
662 ms |
125432 KB |
Output is correct |
49 |
Correct |
654 ms |
127532 KB |
Output is correct |
50 |
Correct |
738 ms |
128256 KB |
Output is correct |
51 |
Correct |
747 ms |
128388 KB |
Output is correct |
52 |
Correct |
738 ms |
125764 KB |
Output is correct |
53 |
Correct |
739 ms |
125612 KB |
Output is correct |
54 |
Correct |
732 ms |
125392 KB |
Output is correct |
55 |
Correct |
737 ms |
127080 KB |
Output is correct |