Submission #1161688

#TimeUsernameProblemLanguageResultExecution timeMemory
1161688I_FloPPed21Growing Trees (BOI11_grow)C++20
100 / 100
432 ms5840 KiB
#include <iostream> #include <algorithm> using namespace std; const int N=1e5+5; long long aint[4*N],lazy[4*N],n,q,v[N]; void propaga(int nod) { lazy[nod*2]+=lazy[nod]; lazy[nod*2+1]+=lazy[nod]; aint[nod*2]+=lazy[nod]; aint[nod*2+1]+=lazy[nod]; lazy[nod]=0; } void update(int nod,int st,int dr,int l,int r,long long val) { if(l>r) return; if(l<=st&&dr<=r) { aint[nod]+=val; lazy[nod]+=val; } else if(l>dr||st>r) { return; } else { int mij=(st+dr)/2; propaga(nod); update(nod*2,st,mij,l,r,val); update(nod*2+1,mij+1,dr,l,r,val); } } long long query(int nod,int st,int dr,int poz) { if(st==dr) { return aint[nod]; } else { propaga(nod); int mij=(st+dr)/2; if(poz<=mij) return query(nod*2,st,mij,poz); else return query(nod*2+1,mij+1,dr,poz); } } void citeste() { cin>>n>>q; for(int i=1; i<=n; i++) cin>>v[i]; sort(v+1,v+n+1); for(int i=1; i<=n; i++) { update(1,1,n,i,i,v[i]); } } void queries() { for(int i=1; i<=q; i++) { char d; cin>>d; if(d=='F') { long long siz,height; cin>>siz>>height; int st=1,dr=n; int poz=-1; while(st<=dr) { int mij=(st+dr)/2; if(query(1,1,n,mij)>=height) { poz=mij; dr=mij-1; } else st=mij+1; } if(poz==-1) continue; if(n-poz+1<=siz) { update(1,1,n,poz,n,1); continue; } long long val=query(1,1,n,poz+siz-1); int poz2=-1; st=poz,dr=poz+siz-1; while(st<=dr) { int mij=(st+dr)/2; if(query(1,1,n,mij)<val) { poz2=mij; st=mij+1; } else dr=mij-1; } update(1,1,n,poz,poz2,1); if(poz2>=poz) { siz-=(poz2-poz+1); } st=1,dr=n; poz2=-1; while(st<=dr) { int mij=(st+dr)/2; if(query(1,1,n,mij)<=val) { poz2=mij; st=mij+1; } else dr=mij-1; } update(1,1,n,poz2-siz+1,poz2,1); } else { long long val1,val2; cin>>val1>>val2; int st=1,dr=n; int poz1=-1,poz2=-1; while(st<=dr) { int mij=(st+dr)/2; if(query(1,1,n,mij)>=val1) { dr=mij-1; poz1=mij; } else st=mij+1; } if(poz1==-1||query(1,1,n,poz1)>val2) { cout<<0<<'\n'; continue; } st=1,dr=n; while(st<=dr) { int mij=(st+dr)/2; if(query(1,1,n,mij)<=val2) { st=mij+1; poz2=mij; } else dr=mij-1; } cout<<poz2-poz1+1<<'\n'; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); citeste(); queries(); return 0; }
#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...