제출 #529178

#제출 시각아이디문제언어결과실행 시간메모리
529178Fake4Fun3단 점프 (JOI19_jumps)C++14
100 / 100
922 ms98976 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...