Submission #1038487

# Submission time Handle Problem Language Result Execution time Memory
1038487 2024-07-29T21:07:42 Z Vectors_Master Fire (JOI20_ho_t5) C++17
0 / 100
27 ms 121184 KB
#include<bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define LL long long
#define LD long double
#define pb push_back
#define F first
#define S second
const double PI=3.1415926535897932384626433;
const int KL=1e6+10;
const LL MOD=1e9+7;
using namespace std;


int n,q,a[KL],prv[KL],nxt[KL];

void calc_prev_nxt() {
    vector<int> vec;
    a[0]=1e9+1;
    vec.pb(0);
    for(int i=1;i<=n;i++) {
        while(a[vec.back()]<a[i])vec.pop_back();
        prv[i]=vec.back();
        vec.pb(i);
    }

    vec.clear();
    a[n+1]=1e9+1;
    vec.pb(n+1);
    for(int i=n;i>=1;i--) {
        while(a[vec.back()]<=a[i])vec.pop_back();
        nxt[i]=vec.back();
        vec.pb(i);
    }
}

vector<int> stop_increase[KL],start_decrease[KL],zero[KL];

int l[KL],r[KL],T[KL],cur[KL];
vector<int> adj[KL],stop[KL];
LL ans[KL];


LL cnst[KL],sub[KL],add[KL];

void update_cnst(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){cnst[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_cnst(pos,new_val,2*cur,st,mid);
    else update_cnst(pos,new_val,2*cur+1,mid+1,en);
    cnst[cur]=cnst[2*cur]+cnst[2*cur+1];
}
void update_cnst_add(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){cnst[cur]+=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_cnst_add(pos,new_val,2*cur,st,mid);
    else update_cnst_add(pos,new_val,2*cur+1,mid+1,en);
    cnst[cur]=cnst[2*cur]+cnst[2*cur+1];
}
void update_add(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){add[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_add(pos,new_val,2*cur,st,mid);
    else update_add(pos,new_val,2*cur+1,mid+1,en);
    add[cur]=add[2*cur]+add[2*cur+1];
}
void update_sub(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){sub[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_sub(pos,new_val,2*cur,st,mid);
    else update_sub(pos,new_val,2*cur+1,mid+1,en);
    sub[cur]=sub[2*cur]+sub[2*cur+1];
}

LL query(int l,int r,LL t,int cur=1,int st=1,int en=n) {
    if(l>en || r<st)return 0;
    if(l<=st && en<=r)return cnst[cur]+(LL)(t+1LL)*add[cur]+(LL)t*sub[cur];
    int mid=(st+en)/2;
    return query(l,r,t,2*cur,st,mid)+query(l,r,t,2*cur+1,mid+1,en);
}


pair<int,int> spt[KL][20];
int llen[KL];

pair<int,int> mrg(pair<int,int> A,pair<int,int> B) {
    if(A.F>=B.F)return A;
    return B;
}
void buildspt() {
    for(int i=1;i<=n;i++) {
        spt[i][0]={a[i],i};
        if(i>=2)llen[i]=llen[i/2]+1;
    }

    for(int j=1;(1<<j)<=n;j++) {
        for(int i=1;i+(1<<j)-1<=n;i++) {
            spt[i][j]=mrg(spt[i][j-1],spt[i+(1<<(j-1))][j-1]);
        }
    }
}
int sptquery(int l,int r) {
    int len=llen[r-l+1];
    return mrg(spt[l][len],spt[r-(1<<len)+1][len]).S;
}

LL query_prefix(int pos,LL t) {
    if(pos==n)return query(1,n,t);
    if(pos==0)return 0;
    int l=max(pos-(int) t,1);
    int element=sptquery(l,pos);
    int exp=min(nxt[element]-1,element+(int)t);
    if(exp<pos) {

    }
    assert(exp>=pos);
    assert(element<=pos);
    // cout<<element<<" "<<a[element]<<" "<<exp<<" "<<pos<<" "<<query(1,element,t)<<"\n";
    return query(1,element,t)-(LL)(exp-pos)*(LL)a[element];

}
void solveTS(){
    cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];
    buildspt();

    calc_prev_nxt();
    for(int i=1;i<=n;i++) {
        int posInc=nxt[i]-i;
        stop_increase[posInc].pb(i);
        int posDec=n+1;
        if(prv[i]!=0) {
            posDec=i-prv[i];
            start_decrease[posDec].pb(i);
        }
        if(posDec<posInc) {
            int to_be_deleted=posInc+posDec-1;
            zero[to_be_deleted].pb(i);
        }
        else {
            int to_be_deleted=posDec+posInc-1;
            zero[to_be_deleted].pb(i);
        }
    }



    for(int qr=1;qr<=q;qr++) {
        cin>>T[qr]>>l[qr]>>r[qr];
        adj[T[qr]].pb(qr);
    }

    // cout<<"stop: \n";
    // for(int t=0;t<=n;t++) {
    //     cout<<"at time "<<t<<": ";
    //     for(auto v:stop_increase[t])cout<<v<<" ";cout<<"\n";
    // }
    //
    // cout<<"\nstart: \n";
    // for(int t=0;t<=n;t++) {
    //     cout<<"at time "<<t<<": ";
    //     for(auto v:start_decrease[t])cout<<v<<" ";cout<<"\n";
    // }
    //
    //
    // cout<<"\nzero: \n";
    // for(int t=0;t<=n;t++) {
    //     cout<<"at time "<<t<<": ";
    //     for(auto v:zero[t])cout<<v<<" ";cout<<"\n";
    // }

    for(int i=1;i<=n;i++)update_add(i,a[i]);

    for(int t=0;t<=n;t++) {
        for(auto i:stop_increase[t]) {
            update_cnst(i,(LL)t*(LL)a[i]);
            update_add(i,0);
        }
        for(auto i:start_decrease[t]) {
            update_cnst_add(i,(t-1LL)*(LL)a[i]);
            update_sub(i,-a[i]);
        }
        for(auto i:zero[t]) {
            update_cnst(i,0);
            update_sub(i,0);
        }

        for(auto qr:adj[t]) {
            // cout<<l[qr]<<" "<<r[qr]<<" "<<t<<"\n";

            ans[qr]=query_prefix(r[qr],t)-query_prefix(l[qr]-1,t);
        }
    }



    for(int qr=1;qr<=q;qr++) {
        cout<<ans[qr]<<"\n";
    }



}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int TS=1;
    // cin>>TS;
    while(TS--){
        solveTS();
    }
    return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:208:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  208 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:209:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  209 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 121180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 121180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 121180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 121184 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 121180 KB Output isn't correct
2 Halted 0 ms 0 KB -