Submission #816004

#TimeUsernameProblemLanguageResultExecution timeMemory
816004alvingogoDistributing Candies (IOI21_candies)C++17
100 / 100
336 ms49776 KiB
#include "candies.h" #include <bits/stdc++.h> #define fs first #define sc second #define p_q priority_queue using namespace std; typedef long long ll; struct no{ ll sum=0; ll mn=0,mx=0; ll ans=0; }; struct ST{ vector<no> st; void init(int x){ st.resize(4*x); } void merge(no& v,no& a,no& b){ v.sum=a.sum+b.sum; v.mn=min(b.mn,b.sum+a.mn); v.mx=max(b.mx,b.sum+a.mx); v.ans=max({a.ans,b.ans,(b.sum+a.mx)-b.mn,b.mx-(b.sum+a.mn)}); } void update(int v,int L,int R,int p,int x){ if(L==R){ st[v].sum=x; st[v].mn=st[v].mx=x; return; } int m=(L+R)/2; if(p<=m){ update(2*v+1,L,m,p,x); } else{ update(2*v+2,m+1,R,p,x); } merge(st[v],st[2*v+1],st[2*v+2]); } no find(int v,int L,int R,int c,no& a){ if(L==R){ no h; merge(h,st[v],a); return h; } int m=(L+R)/2; no h; merge(h,st[2*v+2],a); if(h.ans>c){ return find(2*v+2,m+1,R,c,a); } else{ a=h; return find(2*v+1,L,m,c,a); } } }st; vector<int> distribute_candies(vector<int> w, vector<int> l, vector<int> r, vector<int> v) { int n=w.size(); vector<int> ans(n); int q=l.size()+2; st.init(q); st.update(0,0,q-1,0,2e9); vector<vector<pair<int,int> > > upd(n),del(n); for(int i=0;i<q-2;i++){ upd[l[i]].push_back({i+1,v[i]}); del[r[i]].push_back({i+1,v[i]}); } for(int i=0;i<n;i++){ for(auto h:upd[i]){ st.update(0,0,q-1,h.fs,-h.sc); } no e; auto d=st.find(0,0,q-1,w[i],e); if(d.mx-e.mn>w[i]){ ans[i]=-e.mn; } else{ ans[i]=w[i]-e.mx; } for(auto h:del[i]){ st.update(0,0,q-1,h.fs,0); } } return ans; }
#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...