제출 #439381

#제출 시각아이디문제언어결과실행 시간메모리
439381paliloGrowing Trees (BOI11_grow)C++17
100 / 100
89 ms2964 KiB
#include <bits/stdc++.h> using namespace std; template <typename T> class BIT { const int n; vector<T> tree; public: BIT(int _n) : n(_n), tree(n + 1) {} void update(int i, T val) { assert(0 <= i and i <= n); for (++i; i <= n; i += i & -i) tree[i] += val; } T query(int i) { assert(0 <= i and i <= n); T ret = 0; for (; i; i &= i - 1) ret += tree[i]; return ret; } T query(int l, int r) { assert(0 <= l and l <= r and r <= n); return query(r) - query(l); } T get(int i) { assert(0 <= i and i < n); return i & 1 ? query(i, i + 1) : tree[i + 1]; } int lower_bound(T k) { int x = 0; for (int pw = 1 << __lg(n); pw; pw >>= 1) if ((x | pw) <= n && tree[x | pw] < k) k -= tree[x |= pw]; return x; } int upper_bound(T k) { if (k < 0) return -1; int x = 0; for (int pw = 1 << __lg(n); pw; pw >>= 1) if ((x | pw) <= n && tree[x | pw] <= k) k -= tree[x |= pw]; return x; } }; int main() { cin.tie(nullptr)->sync_with_stdio(false); #ifdef palilo freopen("in", "r", stdin); freopen("out", "w", stdout); #endif int n, m; cin >> n >> m; vector<int> a(n); for (auto& x : a) cin >> x; sort(a.begin(), a.end()); BIT<int> bit(n); for (int i = 0; i < n; ++i) bit.update(i, a[i] - (i ? a[i - 1] : 0)); for (int x, y; m--;) { char op; cin >> op >> x >> y; if (op == 'F') { const int it = bit.lower_bound(y); if (it + x < n) { const int last_value = bit.query(it + x); const int lo = bit.lower_bound(last_value); const int hi = bit.upper_bound(last_value); bit.update(lo, -1); bit.update(hi - (x - (lo - it)), 1); bit.update(hi, -1); } bit.update(it, 1); } else { cout << bit.upper_bound(y) - bit.lower_bound(x) << '\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...