Submission #1104109

#TimeUsernameProblemLanguageResultExecution timeMemory
1104109Kirill22Growing Trees (BOI11_grow)C++17
100 / 100
160 ms5600 KiB
#include "bits/stdc++.h" using namespace std; const int N = (int) 1e5 + 22; int t[4 * N], d[4 * N], a[N]; void push(int v, int l, int r) { t[v] += d[v]; if (l + 1 != r) { d[v * 2 + 1] += d[v]; d[v * 2 + 2] += d[v]; } d[v] = 0; } void update(int v, int l, int r, int l1, int r1, int x) { push(v, l, r); if (l1 >= r || l >= r1) { return; } if (l1 <= l && r <= r1) { d[v] = x; push(v, l, r); return; } int m = (l + r) / 2; update(v * 2 + 1, l, m, l1, r1, x); update(v * 2 + 2, m, r, l1, r1, x); t[v] = max(t[v * 2 + 1], t[v * 2 + 2]); } void build(int v, int l, int r) { if (l + 1 == r) { t[v] = a[l]; return; } int m = (l + r) / 2; build(v * 2 + 1, l, m); build(v * 2 + 2, m, r); t[v] = max(t[v * 2 + 1], t[v * 2 + 2]); } int lower(int v, int l, int r, int x) { push(v, l, r); if (t[v] < x) { return r; } if (l + 1 == r) { return l; } int m = (l + r) / 2; push(v * 2 + 1, l, m); push(v * 2 + 2, m, r); if (t[v * 2 + 1] >= x) { return lower(v * 2 + 1, l, m, x); } else { return lower(v * 2 + 2, m, r, x); } } int get(int v, int l, int r, int l1, int r1) { push(v, l, r); if (l1 >= r || l >= r1) { return 0; } if (l1 <= l && r <= r1) { return t[v]; } int m = (l + r) / 2; return max(get(v * 2 + 1, l, m, l1, r1), get(v * 2 + 2, m, r, l1, r1)); } void solve() { int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); build(0, 0, n); while (q--) { char c; cin >> c; if (c == 'C') { int l, r; cin >> l >> r; int posl = lower(0, 0, n, l); int posr = lower(0, 0, n, r + 1); cout << posr - posl << '\n'; } else { int cnt, x; cin >> cnt >> x; int pos = lower(0, 0, n, x); if (n - pos <= cnt) { update(0, 0, n, pos, n, 1); continue; } int val = get(0, 0, n, pos + cnt - 1, pos + cnt); int pos2 = lower(0, 0, n, val); update(0, 0, n, pos, pos2, 1); cnt -= pos2 - pos; int pos3 = lower(0, 0, n, val + 1); update(0, 0, n, pos3 - cnt, pos3, 1); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
#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...