Submission #619674

# Submission time Handle Problem Language Result Execution time Memory
619674 2022-08-02T14:27:17 Z Koosha_mv Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 41880 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,int(v.size())) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;
const ll inf=1e16;

int n,q,c[N],a[N];
ll Min[N<<2],Max[N<<2],lz[N<<2];
ll b[N];
vector<int> s,t,val,ad[N],dl[N];

void upd(int id){
    Min[id]=min(Min[id<<1],Min[id<<1|1]);
    Max[id]=max(Max[id<<1],Max[id<<1|1]);
}
void shift(int id){
    lz[id<<1]+=lz[id];
    Min[id<<1]+=lz[id];
    Max[id<<1]+=lz[id];

    lz[id<<1|1]+=lz[id];
    Min[id<<1|1]+=lz[id];
    Max[id<<1|1]+=lz[id];

    lz[id]=0;
}

void add(int L,int R,ll val,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return ;
    if(L<=l && r<=R){
        lz[id]+=val;
        Min[id]+=val;
        Max[id]+=val;
        return ;
    }
    int mid=(l+r)>>1;
    shift(id);
    add(L,R,val,id<<1,l,mid);
    add(L,R,val,id<<1|1,mid,r);
    upd(id);
}
int find(ll d,ll mn=inf,ll mx=-inf,int id=1,int l=0,int r=q+1){
    if(Max[1]-Min[1]<d) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(max(mx,Max[id<<1|1])-min(mn,Min[id<<1|1])<d){
        return find(d,min(mn,Min[id<<1|1]),max(mx,Max[id<<1|1]),id<<1,l,mid);
    }
    else{
        return find(d,mn,mx,id<<1|1,mid,r);
    }
}
ll get0(int L,int R,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return inf;
    if(L<=l && r<=R){
        return Min[id];
    }
    int mid=(l+r)>>1;
    shift(id);
    return min(get0(L,R,id<<1,l,mid),get0(L,R,id<<1|1,mid,r));
}
ll get1(int L,int R,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return -inf;
    if(L<=l && r<=R){
        return Max[id];
    }
    int mid=(l+r)>>1;
    shift(id);
    return max(get1(L,R,id<<1,l,mid),get1(L,R,id<<1|1,mid,r));
}
int find0(int d,int id=1,int l=0,int r=q+1){
    if(d<=Min[1]) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(Min[id<<1|1]>=d){
        return find0(d,id<<1,l,mid);
    }
    else{
        return find0(d,id<<1|1,mid,r);
    }
}
int find1(int d,int id=1,int l=0,int r=q+1){
    if(d>=Max[1]) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(Max[id<<1|1]<=d){
        return find1(d,id<<1,l,mid);
    }
    else{
        return find1(d,id<<1|1,mid,r);
    }
}

vector<int> distribute_candies(vector<int> _c,vector<int> _s,vector<int> _t,vector<int> _val){
    s=_s,t=_t,val=_val;
    n=_c.size(),q=s.size();
    vector<int> ans(n);
    f(i,0,n) c[i]=_c[i];
    f(i,0,q){
        ad[s[i]].pb(i);
        dl[t[i]].pb(i);
    }
    f(i,0,n){
        for(auto x : ad[i]) a[x+1]+=val[x];
        for(auto x : ad[i]) add(x+1,q+1,val[x]);
        ll sum=0;
        f(j,1,q+1){
            b[j]=b[j-1]+a[j];
            sum+=a[j];
            maxm(sum,0ll);
            minm(sum,1ll*c[i]);
        }
        ans[i]=sum;
        //dbga(b,0,q+1);
        int p=find(c[i]);
        int b0=find0(get0(p,q+1));
        int b1=find1(get1(p,q+1));
        //cout<<p<<" - "<<b0<<" "<<b1<<endl;
        ll mn=get0(p,q+1);
        ll mx=get1(p,q+1);

        
        ans[i]=get0(q,q+1)-get0(p,q+1);
        f_(j,p-1,0){
            if(b[j]<mn){
                ans[i]=c[i]-(get1(p,q+1)-get1(q,q+1));
                break ;
            }
            if(b[j]>mx){
                break ;
            }
        }

        for(auto x : dl[i]) add(x+1,q+1,-val[x]);
        for(auto x : dl[i]) a[x+1]-=val[x];
    }
    return ans;
}
/*
3
10 15 13
2
0 2 20
0 1 -11

*/

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:145:13: warning: unused variable 'b0' [-Wunused-variable]
  145 |         int b0=find0(get0(p,q+1));
      |             ^~
candies.cpp:146:13: warning: unused variable 'b1' [-Wunused-variable]
  146 |         int b1=find1(get1(p,q+1));
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9940 KB Output is correct
4 Correct 7 ms 9940 KB Output is correct
5 Correct 20 ms 10036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5027 ms 41848 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 1412 ms 34332 KB Output is correct
3 Correct 1296 ms 14280 KB Output is correct
4 Execution timed out 5042 ms 41880 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 1222 ms 33084 KB Output is correct
4 Correct 1258 ms 13196 KB Output is correct
5 Execution timed out 5056 ms 36372 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9940 KB Output is correct
4 Correct 7 ms 9940 KB Output is correct
5 Correct 20 ms 10036 KB Output is correct
6 Execution timed out 5027 ms 41848 KB Time limit exceeded
7 Halted 0 ms 0 KB -