제출 #874429

#제출 시각아이디문제언어결과실행 시간메모리
87442912345678Growing Trees (BOI11_grow)C++17
100 / 100
118 ms4192 KiB
#include <bits/stdc++.h> using namespace std; const int nx=1e5+5; int n, m, x, y; char t; vector<int> v; struct segtree { int d[4*nx], lz[4*nx]; void pushlz(int l, int r, int i) { if (l==r) return d[i]+=lz[i], lz[i]=0, void(); lz[2*i]+=lz[i]; lz[2*i+1]+=lz[i]; d[i]+=lz[i]; lz[i]=0; } void build(int l, int r, int i) { if (l==r) return void(d[i]=v[l-1]); int md=(l+r)/2; build(l, md, 2*i); build(md+1, r, 2*i+1); d[i]=d[2*i+1]; } void update(int l, int r, int i, int ul, int ur) { pushlz(l, r, i); if (r<ul||ur<l) return; if (ul<=l&&r<=ur) return lz[i]++, pushlz(l, r, i), void(); int md=(l+r)/2; update(l, md, 2*i, ul, ur); update(md+1, r, 2*i+1, ul, ur); d[i]=d[2*i+1]; } int query(int l, int r, int i, int idx) { pushlz(l, r, i); if (l==r) return d[i]; int md=(l+r)/2; if (idx<=md) return query(l, md, 2*i, idx); return query(md+1, r, 2*i+1, idx); } int lower_bound(int l, int r, int i, int vl) { pushlz(l, r, i); if (l==r) return l; int md=(l+r)/2; pushlz(l, md ,2*i); pushlz(md+1, r, 2*i+1); if (d[2*i]>=vl) return lower_bound(l, md ,2*i, vl); else return lower_bound(md+1, r, 2*i+1, vl); } void show() { for (int i=1; i<=n+1; i++) cout<<query(1, n+1, 1, i)<<' '; cout<<'\n'; } } s; int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>m; v.resize(n+1); for (int i=0; i<n; i++) cin>>v[i]; v[n]=2e9; sort(v.begin(), v.end()); s.build(1, n+1, 1); //s.show(); for (int i=0; i<m; i++) { cin>>t>>x>>y; if (t=='F') { swap(x, y); int st=s.lower_bound(1, n+1, 1, x), lst=st+y-1; //cout<<"here"<<' '<<st<<' '<<lst<<'\n'; if (lst>=n) s.update(1, n+1, 1, st, n); else { int lvl=s.query(1, n+1, 1, lst), pv=s.lower_bound(1, n+1, 1, lvl)-1, sv=s.lower_bound(1, n+1, 1, lvl+1)-1; int nd=lst-pv; if (nd>=y) s.update(1, n+1, 1, sv-nd+1, sv); else s.update(1, n+1, 1, sv-nd+1, sv), s.update(1, n+1, 1, st, st+y-nd-1); } //s.show(); } else { //cout<<"query"<<' '<<s.lower_bound(1, n+1, 1, y+1)<<' '<<s.lower_bound(1, n+1, 1, x)<<'\n'; cout<<(s.lower_bound(1, n+1, 1, y+1)-s.lower_bound(1, n+1, 1, x))<<'\n'; } } } /* 5 1 1 3 2 5 2 F 2 1 */
#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...