Submission #943845

#TimeUsernameProblemLanguageResultExecution timeMemory
943845tamir1Growing Trees (BOI11_grow)C++17
100 / 100
308 ms5340 KiB
#include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,i,a[100005],x,y,s[400005]; char type; void update(ll node,ll l,ll r,ll L,ll R,ll val){ if(l>R || r<L) return; if(l>=L && r<=R){ s[node]+=val; return; } ll mid=(r+l)/2; update(node*2,l,mid,L,R,val); update(node*2+1,mid+1,r,L,R,val); } ll get(ll node,ll l,ll r,ll loc){ if(l==r) return s[node]; ll mid=(r+l)/2,z; if(loc<=mid) z=get(node*2,l,mid,loc); else z=get(node*2+1,mid+1,r,loc); return s[node]+z; } ll find(ll x,ll y){ ll l,r,mid; if(get(1,1,n,n)<x || get(1,1,n,1)>y) return 0; l=1; r=n; while(r-l>1){ mid=(r+l+1)/2; if(get(1,1,n,mid)>=x) r=mid; else l=mid; } if(get(1,1,n,l)>=x) x=l; else x=r; l=1; r=n; while(r-l>1){ mid=(r+l+1)/2; if(get(1,1,n,mid)>y) r=mid; else l=mid; } if(get(1,1,n,r)<=y) y=r; else y=l; return y-x+1; } void change(ll c,ll h){ if(get(1,1,n,n)<h) return; ll l,r,mid,strt,val1,val2,x,y; l=1; r=n; while(r-l>1){ mid=(r+l+1)/2; if(get(1,1,n,mid)>=h) r=mid; else l=mid; } if(get(1,1,n,l)>=h) strt=l; else strt=r; if(strt+c-1>=n){ update(1,1,n,strt,n,1); return; } val1=get(1,1,n,strt); val2=get(1,1,n,strt+c-1); l=1; r=n; while(r-l>1){ mid=(r+l+1)/2; if(get(1,1,n,mid)>=val2) r=mid; else l=mid; } if(get(1,1,n,l)>=val2) x=l; else x=r; l=1; r=n; while(r-l>1){ mid=(r+l+1)/2; if(get(1,1,n,mid)>val2) r=mid; else l=mid; } if(get(1,1,n,r)<=val2) y=r; else y=l; if(val1!=val2){ update(1,1,n,strt,x-1,1); c=c-(x-strt); update(1,1,n,y-c+1,y,1); } else update(1,1,n,y-c+1,y,1); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for(i=1;i<=n;i++) cin >> a[i]; sort(a+1,a+n+1); for(i=1;i<=n;i++) update(1,1,n,i,i,a[i]); while(m--){ cin >> type >> x >> y; if(type=='C'){ cout << find(x,y) << "\n"; } else{ change(x,y); } } }
#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...
#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...