Submission #1038356

#TimeUsernameProblemLanguageResultExecution timeMemory
1038356ThunnusGrowing Trees (BOI11_grow)C++17
100 / 100
95 ms4204 KiB
#include<bits/stdc++.h> using namespace std; using i64 = long long; #define int i64 #define vi vector<int> #define vvi vector<vi> #define vb vector<bool> #define pii pair<int, int> #define fi first #define se second #define sz(x) (int)(x).size() struct BIT{ vi bit; BIT(int n) : bit(n + 2) {} void add(int idx, int val){ for(++idx; idx < sz(bit); idx += idx & -idx){ bit[idx] += val; } } int sum(int idx){ int ret = 0; for(++idx; idx; idx -= idx & -idx) ret += bit[idx]; return ret; } }; int bs(int lo, int hi, function<bool(int)> check){ hi++; while(lo < hi){ int mid = lo + (hi - lo) / 2; if(check(mid)) hi = mid; else lo = mid + 1; } return lo; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m; char type; cin >> n >> m; vi a(n); BIT bit(n); for(int &i : a) cin >> i; sort(a.begin(), a.end()); auto range_add = [&](int l, int r, int val) -> void { bit.add(l, val); bit.add(r + 1, -val); }; for(int i = 0; i < n; i++){ range_add(i, i, a[i]); } while(m--){ int a, b; cin >> type >> a >> b; if(type == 'C'){ int up_l = bs(0, n - 1, [&](int idx) {return bit.sum(idx) >= a;} ); int low_r = bs(0, n - 1, [&](int idx) {return bit.sum(idx) > b;} ) - 1; cout << low_r - up_l + 1 << "\n"; } else{ int l = bs(0, n - 1, [&](int idx) {return bit.sum(idx) >= b;} ), r = l + a - 1; if(l == n) continue; if(r >= n - 1){ range_add(l, n - 1, 1); continue; } int last_val = bit.sum(r); int l_ = bs(l, n - 1, [&](int idx) {return bit.sum(idx) >= last_val;} ), r_ = bs(l, n - 1, [&](int idx) {return bit.sum(idx) > last_val;} ) - 1; range_add(l, l_ - 1, 1); range_add(r_ - (a - (l_ - 1 - l + 1)) + 1, r_, 1); } } 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...