Submission #1084548

#TimeUsernameProblemLanguageResultExecution timeMemory
1084548BLOBVISGODGrowing Trees (BOI11_grow)C++17
100 / 100
50 ms3280 KiB
#include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; struct fentree{ vi a; void add(int at, int val){ for(at++; at<sz(a); at+=at&(-at)) a[at] += val; }void addrange(int l, int r, int val){ // [ ) add(r,-val); add(l,val); } fentree(vi b){ a.resize(sz(b)+1); rep(i,0,sz(b)) addrange(i,i+1,b[i]); }int get(int at){ int ans = 0; for(at++; at>0; at-=at&(-at)) ans += a[at]; return ans; } int lowerbound(int v){ int at = 0, sm=0; for(int pw = 1<<__lg(sz(a)-1); pw>0; pw/=2){ if (at+pw < sz(a) and sm + a[at+pw] < v){ at += pw; sm += a[at]; } }return at; } }; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int n,m; cin >> n >> m; vi a(n); rep(i,0,n) cin >> a[i]; sort(all(a)); fentree fen(a); // auto lowerbound = [&](int v) -> int { // int lo = 0, hi = sz(a); // while (lo<hi){ // int mid = (lo+hi)/2; // if (fen.get(mid) < v) lo = mid+1; // else hi = mid; // }return lo; // }; auto update = [&](int c, int h) -> void { int L = fen.lowerbound(h); if (sz(a)-L <= c) fen.addrange(L,sz(a),1); else{ int col = fen.get(L+c-1); // find first index of this col int R1 = fen.lowerbound(col); int numleft = c - (R1-L); // get first index larger than col int R = fen.lowerbound(col+1); int L1 = R - numleft; // update everything fen.addrange(L,R1,1); fen.addrange(L1,R,1); } }; auto query = [&](int lo, int hi) -> int { return fen.lowerbound(hi+1)-fen.lowerbound(lo); }; while(m--){ char t; cin >> t; if (t=='F'){ int c,h; cin>> c >> h; update(c,h); }else{ int lo,hi; cin >> lo >> hi; cout << query(lo,hi) << '\n'; } } }
#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...