This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
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){
if(r<=L || R<=l) return -inf;
if(L<=l && r<=R){
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 ;
update(2*x-i,n+1,a[i]+a[x]);
}
for(auto x : Q[i]){
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]<<" ";
}
Compilation message (stderr)
jumps.cpp: In function 'void shift(long long int, long long int, long long int)':
jumps.cpp:99:6: warning: unused variable 'mid' [-Wunused-variable]
99 | int mid=(l+r)>>1;
| ^~~
jumps.cpp: At global scope:
jumps.cpp:106:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
106 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |