Submission #520471

#TimeUsernameProblemLanguageResultExecution timeMemory
520471blueGrowing Trees (BOI11_grow)C++17
0 / 100
40 ms5892 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; using vi = vector<int>; using ll = long long; using vll = vector<ll>; const int mx = 100'000; const int Z = (1<<17); vll delta(1+mx); int N, M; struct segtree { vi l = vi(1+Z); vi r = vi(1+Z); vll sm = vll(1+Z); segtree() { ; } void build(int i, int L, int R) { l[i] = L; r[i] = R; if(l[i] == r[i]) { sm[i] = delta[l[i]]; } else { build(2*i, L, (L+R)/2); build(2*i+1, (L+R)/2+1, R); sm[i] = sm[2*i] + sm[2*i+1]; } } int first_geq(int i, ll s) { // cerr << i << ' ' << l[i] << ' ' << r[i] << " : " << sm[i] << ' ' << s << '\n'; if(sm[i] < s) return N+1; else if(l[i] == r[i]) return l[i]; else if(sm[i<<1] >= s) return first_geq(i<<1, s); else return first_geq((i<<1) + 1, s - sm[i<<1]); } int first_geq(ll s) {return first_geq(1, s);} void add(int i, int I, ll V) { sm[i] += V; if(l[i] != r[i]) { if(I <= (l[i] + r[i])/2) add(2*i, I, V); else add(2*i+1, I, V); } } void add(int I, int V) {add(1, I, V);} int get_sum(int i, int I) { if(l[i] == r[i]) return sm[i]; else if(I <= (l[i] + r[i])/2) return get_sum(i<<1, I); else return sm[(i<<1)] + get_sum((i<<1) + 1, I); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> M; vll init_h(1+N); for(int i = 1; i <= N; i++) cin >> init_h[i]; init_h[0] = 0; sort(init_h.begin(), init_h.end()); for(int i = 1; i <= N; i++) delta[i] = init_h[i] - init_h[i-1]; segtree S; S.build(1, 1, N); for(int q = 1; q <= M; q++) { char t; cin >> t; if(t == 'F') { ll c, h; cin >> c >> h; int stp = S.first_geq(h); if(stp >= N+1) continue; int ep = min(stp + c - 1, (ll)N); ll ep_val = S.get_sum(1, ep); int last_start = S.first_geq(1, ep_val); int last_end = S.first_geq(1, ep_val+1) - 1; int last_count = ep - last_start + 1; S.add(last_end - last_count + 1, +1); S.add(last_end+1, -1); S.add(stp, +1); S.add(last_start, -1); } else { ll mn, mx; cin >> mn >> mx; cout << S.first_geq(1, mx+1) - S.first_geq(1, mn) << '\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...