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;
const int maxn=500000+10;
int n,all[maxn],q,res[maxn],kaf=(1<<19);
vector<pair<int,int>>allq[maxn],updy[maxn];
struct segment{
struct node{
int mx,lazy,res;
node(){
mx=lazy=res=0;
}
};
node seg[(1<<20)];
void calc(int i){
if(i>=kaf){
seg[i].res=max(seg[i].res,seg[i].mx+seg[i].lazy);
seg[i].lazy=0;
return ;
}
seg[i].res=max(seg[i].res,max(seg[(i<<1)].res,seg[(i<<1)^1].res));
seg[i].mx=max(seg[(i<<1)].mx,seg[(i<<1)^1].mx);
}
void shift(int i){
if(i>=kaf){
return calc(i);
}
seg[(i<<1)].lazy=max(seg[i].lazy,seg[(i<<1)].lazy);
seg[(i<<1)^1].lazy=max(seg[i].lazy,seg[(i<<1)^1].lazy);
seg[i].lazy=0;
seg[(i<<1)].res=max(seg[(i<<1)].res,seg[(i<<1)].lazy+seg[(i<<1)].mx);
seg[(i<<1)^1].res=max(seg[(i<<1)^1].res,seg[(i<<1)^1].lazy+seg[(i<<1)^1].mx);
}
void ins(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
// cout<<"ins: "<<l<<" "<<r<<" "<<w<<endl;
calc(i);
seg[i].mx=max(w,seg[i].mx);
seg[i].lazy=0;
seg[i].res=max(seg[i].res,w);
return ;
}
shift(i);
int m=(l+r)>>1;
ins((i<<1),l,m,tl,tr,w);
ins((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
}
void upd(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
// cout<<"upd: "<<l<<" "<<r<<" "<<w<<endl;
seg[i].lazy=max(seg[i].lazy,w);
seg[i].res=max(seg[i].res,seg[i].mx+seg[i].lazy);
return ;
}
shift(i);
int m=(l+r)>>1;
upd((i<<1),l,m,tl,tr,w);
upd((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
}
int pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return 0;
}
if(l>=tl&&r<=tr){
return seg[i].res;
}
shift(i);
calc(i);
int m=(l+r)>>1;
return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}seg;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>all[i];
}
cin>>q;
for(int i=1;i<=q;i++){
int l,r;
cin>>l>>r;
allq[r].push_back(make_pair(l,i));
}
vector<int>v;
for(int i=1;i<=n;i++){
for(auto x:updy[i]){
seg.ins(1,0,kaf-1,x.first,x.first,x.second);
}
seg.upd(1,0,kaf-1,0,i,all[i]);
for(auto x:allq[i]){
res[x.second]=seg.pors(1,0,kaf-1,x.first,i);
}
while((int)v.size()>0&&all[v.back()]<=all[i]){
updy[min(n+1,i+(i-v.back()))].push_back(make_pair(v.back(),all[i]+all[v.back()]));
v.pop_back();
}
if((int)v.size()>0){
updy[min(n+1,i+(i-v.back()))].push_back(make_pair(v.back(),all[i]+all[v.back()]));
}
v.push_back(i);
}
for(int i=1;i<=q;i++){
cout<<res[i]<<"\n";
}
}
# | 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... |