Submission #1038940

#TimeUsernameProblemLanguageResultExecution timeMemory
1038940TrinhKhanhDungGrowing Trees (BOI11_grow)C++14
10 / 100
97 ms9040 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} struct SegmentTree{ vector<ll> val, lazy; int n; SegmentTree(int _n = 0){ n = _n; val.resize(4 * n + 3, 0); lazy.resize(4 * n + 3, 0); } void push(int i){ lazy[i * 2] += lazy[i]; lazy[i * 2 + 1] += lazy[i]; val[i * 2] += lazy[i]; val[i * 2 + 1] += lazy[i]; lazy[i] = 0; } void update(int i, int l, int r, int u, int v, ll c){ if(v < l || r < u) return; if(u <= l && r <= v){ val[i] += c; lazy[i] += c; return; } push(i); int m = (l + r) >> 1; update(i * 2, l, m, u, v, c); update(i * 2 + 1, m + 1, r, u, v, c); val[i] = max(val[i * 2], val[i * 2 + 1]); } void update(int u, int v, ll c){ update(1, 1, n, u, v, c); } int getPos(ll c){ if(val[1] < c) return n + 1; int i = 1, l = 1, r = n; while(l < r){ push(i); int m = (l + r) >> 1; if(val[i * 2] >= c){ i = i * 2; r = m; } else{ i = i * 2 + 1; l = m + 1; } } return l; } ll getVal(int p){ int i = 1, l = 1, r = n; while(l < r){ push(i); int m = (l + r) >> 1; if(p <= m){ i = i * 2; r = m; } else{ i = i * 2 + 1; l = m + 1; } } return val[i]; } }; void solve(){ int n, q; cin >> n >> q; SegmentTree it(n); vector<int> a(n + 3); for(int i=1; i<=n; i++){ cin >> a[i]; } sort(a.begin() + 1, a.begin() + n + 1); for(int i=1; i<=n; i++){ it.update(i, i, a[i]); } while(q--){ char t; cin >> t; ll u, v; cin >> u >> v; if(t == 'F'){ int l = it.getPos(v); minimize(u, n - l + 1); ll c = it.getVal(l + u - 1); int r = it.getPos(c) - 1; it.update(l, r, 1); int R = it.getPos(c + 1) - 1; int L = R - (u - (r - l + 1)) + 1; it.update(L, R, 1); } else{ int l = it.getPos(u); int r = it.getPos(v + 1); cout << r - l << '\n'; } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("task.inp", "r", stdin); // freopen("task.out", "w", stdout); int t = 1; // cin >> t; while(t--){ solve(); } 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...