Submission #1153178

#TimeUsernameProblemLanguageResultExecution timeMemory
1153178brover29Triple Jump (JOI19_jumps)C++20
0 / 100
162 ms34908 KiB
#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
const ll N=5e5+29;
const string br="617283";
#define sz(a)(ll)a.size()
#define f first
#define s second
ll n,q,w[N],st[N][20],lg[N];
void build(){
    lg[1]=0;
    for(ll i=2;i<=n;i++)lg[i]=lg[i/2]+1;
    for(ll j=1;j<20;j++){
        ll k=(1ll<<j);
        for(ll i=k-1;i<=n;i++){
            st[i][j]=max(st[i][j-1],st[i-k/2][j-1]);
        }
    }
}
ll get(ll l,ll r){
    if(l>r)return -1e18;
    ll k=lg[r-l+1];
    return max(st[r][k],st[l+(1ll<<k)-1][k]);
}ll check(ll a,ll b,ll r){
    ll x=b-a;
    return w[a]+w[b]+get(b+x,r);
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>w[i];
        st[i][0]=w[i];
    }ll q;
    cin>>q;
    build();
    while(q--){
        ll l,r,ans=0;
        cin>>l>>r;
        for(ll a=l;a<=r-2;a++){
            ll L=a+1,R=r-1;
            while(R-L>3){
                ll m1=L+(R-L)/3;
                ll m2=R-(R-L)/3;
                if(check(a,m1,r)>check(a,m2,r))R=m2;
                else L=m1;
            }
            for(ll i=L;i<=R;i++)ans=max(ans,check(a,i,r));
           // cout<<'\n';
        }
        cout<<ans<<'\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...