이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<vector>
#define pa pair<int,int>
#define fi first
#define se second
using namespace std;
using ui=unsigned int;
const int N=5e5+5;
int n,q;
int a[N],p[N];
vector<int> wait[N];
vector<pa> que[N];
int xl[N<<2],xr[N<<2];
bool ok[N<<2];
ui IT[N<<2],ma[N<<2],ans[N];
int lazy[N<<2];
void build(int x=1,int l=1,int r=n){
xl[x]=l, xr[x]=r;
if(l==r){ma[x]=a[l]; return;}
int xm=(l+r)>>1;
build(x<<1,l,xm); build(x<<1|1,xm+1,r);
ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
void fix(int x,int C){
if(C<=lazy[x]) return;
ok[x]=1, lazy[x]=C;
IT[x]=max(IT[x],ma[x]+C);
}
void down(int x){
for(int i=x<<1;i<=(x<<1|1);i++)
fix(i,lazy[x]);
ok[x]=0;
}
void update(int L,int C,int x=1){
if(C<=lazy[x]) return;
if(L<=xl[x]){fix(x,C); return;}
if(ok[x]) down(x);
if(xr[x<<1]>=L) update(L,C,x<<1);
update(L,C,x<<1|1);
IT[x]=max(IT[x<<1],IT[x<<1|1]);
}
ui query(int L,int R,int x=1){
if(xr[x]<L||R<xl[x]) return 0;
if(L<=xl[x]&&xr[x]<=R) return IT[x];
if(ok[x]) down(x);
return max(query(L,R,x<<1),query(L,R,x<<1|1));
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
// freopen("tripos.inp","r",stdin);
// freopen("tripos.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]; p[i]=i-1;
while(p[i]&&a[p[i]]<a[i]) p[i]=p[p[i]];
if(p[i]) wait[p[i]].push_back(i);
}
for(int i=n;i>=1;i--){
p[i]=i+1;
while(p[i]<=n&&a[p[i]]<a[i]) p[i]=p[p[i]];
if(p[i]<=n&&a[i]<a[p[i]]) wait[i].push_back(p[i]);
}
build();
cin>>q;
int l,r;
for(int i=1;i<=q;i++){
cin>>l>>r;
que[l].emplace_back(r,i);
}
for(int i=n;i>=1;i--){
for(auto j:wait[i])
if(2*j-i<=n) update(2*j-i,a[i]+a[j]);
for(auto x:que[i]) ans[x.se]=query(i,x.fi);
}
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
return 0;
}
# | 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... |