Submission #834845

#TimeUsernameProblemLanguageResultExecution timeMemory
834845JakobZorzDistributing Candies (IOI21_candies)C++17
100 / 100
552 ms41676 KiB
#include"candies.h" #include<iostream> #include<vector> #include<limits.h> using namespace std; typedef long long ll; ll n,days; const ll TREE_SIZE=1<<18; vector<ll>mn; vector<ll>mx; vector<ll>lazy; void tree_init(){ mn.resize(TREE_SIZE*2); mx.resize(TREE_SIZE*2); lazy.resize(TREE_SIZE*2); } void update_lazy(ll node){ if(lazy[node]==0) return; mx[node]+=lazy[node]; mn[node]+=lazy[node]; if(node<TREE_SIZE){ lazy[2*node]+=lazy[node]; lazy[2*node+1]+=lazy[node]; } lazy[node]=0; } void update_node(ll node){ update_lazy(node); if(node>=TREE_SIZE){ mn[node]=mx[node]; return; } update_lazy(2*node); update_lazy(2*node+1); mn[node]=min(mn[2*node],mn[2*node+1]); mx[node]=max(mx[2*node],mx[2*node+1]); } void tree_add(ll node,ll l,ll r,ll pos,ll val){ //cout<<l<<" "<<r<<endl; if(r<=pos) return; if(pos<=l){ //if(node>=TREE_SIZE){ lazy[node]+=val; update_node(node); return; } ll m=(l+r)/2; tree_add(2*node,l,m,pos,val); tree_add(2*node+1,m,r,pos,val); update_node(node); } void tree_add(ll pos,ll val){ tree_add(1,0,TREE_SIZE,pos,val); } pair<pair<ll,ll>,ll>tree_get2(ll node,ll l,ll r,ll p_max,ll p_min,ll bound){ //cout<<l<<" "<<r<<"\n"; update_node(node); if(node>=TREE_SIZE) return {{max(p_max,mx[node]),min(p_min,mn[node])},l}; ll p_max2=max(p_max,mx[2*node+1]); ll p_min2=min(p_min,mn[2*node+1]); //cout<<"pmax2 "<<p_max2<<" pmin2 "<<p_min2<<"\n"; ll m=(l+r)/2; if(p_max2-p_min2<bound){ return tree_get2(2*node,l,m,p_max2,p_min2,bound); }else{ return tree_get2(2*node+1,m,r,p_max,p_min,bound); } } ll get_idx(ll node,ll l,ll r,ll idx){ update_node(node); if(node>=TREE_SIZE){ //cout<<"node "<<node<<endl; return mn[node]; } ll m=(l+r)/2; if(idx>=m) return get_idx(2*node+1,m,r,idx); else return get_idx(2*node,l,m,idx); } ll tree_get(ll bound){ /*for(int i=0;i<days;i++) cout<<mn[TREE_SIZE+i]<<" "; cout<<endl;*/ update_node(1); if(mx[1]-mn[1]<bound){ //cout<<"a"<<endl; return get_idx(1,0,TREE_SIZE,days-1)-mn[1]; } auto res=tree_get2(1,0,TREE_SIZE,LLONG_MIN,LLONG_MAX,bound); //cout<<res.first.first<<" "<<res.first.second<<" "<<res.second<<"\n";= /*cout<<"res.second "<<TREE_SIZE+res.second<<endl; cout<<get_idx(1,0,TREE_SIZE,res.second)<<endl; cout<<mn[TREE_SIZE+res.second]<<endl; cout<<endl;*/ if(res.first.first==get_idx(1,0,TREE_SIZE,res.second)){ //cout<<"b"<<endl; return get_idx(1,0,TREE_SIZE,days-1)-res.first.second; }else{ //cout<<"c "<<bound<<" "<<bound-(res.first.first-get_idx(1,0,TREE_SIZE,days-1))<<endl; return bound-(res.first.first-get_idx(1,0,TREE_SIZE,days-1)); } } vector<int>distribute_candies(vector<int>c,vector<int>l,vector<int>r,vector<int>v){ n=c.size(); days=v.size()+1; vector<vector<pair<ll,ll>>>events; events.resize(n+1); for(ll i=0;i<days-1;i++){ events[l[i]].push_back({i+1,v[i]}); events[r[i]+1].push_back({i+1,-v[i]}); } vector<int>s(n); tree_init(); for(ll i=0;i<n;i++){ for(auto event:events[i]) tree_add(event.first,event.second); s[i]=tree_get(c[i]); } //return {}; return s; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...