Submission #94189

#TimeUsernameProblemLanguageResultExecution timeMemory
94189rkocharyanGrowing Trees (BOI11_grow)C++14
100 / 100
750 ms4760 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m; int t[4 * N]; int lazy[4 * N]; void push(int v, int tl, int tr) { if(lazy[v]) { t[v] += lazy[v] * (tr - tl + 1); if(tl != tr) { lazy[2 * v + 1] += lazy[v]; lazy[2 * v + 2] += lazy[v]; } lazy[v] = 0; } } void update(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if(l > r) return; if(tl == l && tr == r) { lazy[v]++; push(v, tl, tr); return; } int tm = (tl + tr) >> 1; update(2 * v + 1, tl, tm, l, min(r, tm)); update(2 * v + 2, tm + 1, tr, max(l, tm + 1), r); t[v] = t[2 * v + 1] + t[2 * v + 2]; } int get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if(l > r) return 0; if(tl == l && tr == r) return t[v]; int tm = (tl + tr) >> 1; int lf = get(2 * v + 1, tl, tm, l, min(r, tm)); int rg = get(2 * v + 2, tm + 1, tr, max(l, tm + 1), r); return lf + rg; } int get(int pos) { return get(0, 1, n, pos, pos); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; vector <int> a(n + 1, 0); for(int i = 1; i <= n; i++) { cin >> a[i]; } a[0] = -1; sort(a.begin(), a.end()); while(m--) { char type; cin >> type; if(type == 'F') { int c, h; cin >> c >> h; int low = 1, high = n, pos = n + 1; while(low <= high) { int mid = (low + high) >> 1; if(a[mid] + get(mid) >= h) { pos = mid; high = mid - 1; } else { low = mid + 1; } } if(pos == n + 1) continue; int to = pos + c - 1; to = min(n, to); if(to == n || a[to + 1] + get(to + 1) != a[to] + get(to)) { update(0, 1, n, pos, to); } else { int f = a[to] + get(to); low = 1, high = n; int l = 0; while(low <= high) { int mid = (low + high) >> 1; if(a[mid] + get(mid) < f) { l = mid; low = mid + 1; } else { high = mid - 1; } } low = 1, high = n; int r = n + 1; while(low <= high) { int mid = (low + high) >> 1; if(a[mid] + get(mid) > f) { r = mid; high = mid - 1; } else { low = mid + 1; } } l++; r--; int len = to - l + 1; update(0, 1, n, pos, l - 1); update(0, 1, n, r - len + 1, r); } } else { int mn, mx; cin >> mn >> mx; int low = 1, high = n, l = n + 1; while(low <= high) { int mid = (low + high) >> 1; if(a[mid] + get(mid) >= mn) { l = mid; high = mid - 1; } else { low = mid + 1; } } low = 1, high = n; int r = 0; while(low <= high) { int mid = (low + high) >> 1; if(a[mid] + get(mid) <= mx) { r = mid; low = mid + 1; } else { high = mid - 1; } } if(l > r) { cout << "0\n"; continue; } cout << r - l + 1 << '\n'; } } 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...