Submission #768521

#TimeUsernameProblemLanguageResultExecution timeMemory
768521PurpleCrayonDistributing Candies (IOI21_candies)C++17
3 / 100
5156 ms1720592 KiB
#include "candies.h" #include <bits/stdc++.h> using namespace std; #define sz(v) int(v.size()) #define ar array typedef long long ll; const int N = 2e5+10, MOD = 1e9+7; const int B = 3, K = N / B + 10; int n, a[N], cap[N]; vector<int> lzy[K]; vector<pair<int, int>> sorted[K]; int store[N]; void push_block(int block) { const auto& v = lzy[block]; int q = sz(v); if (!q) return; vector<ll> ps(q); for (int i = 0; i < q; i++) { ps[i] = v[i]; if (i) ps[i] += ps[i-1]; } vector<ll> mn(q), mx(q); for (int i = q-1; i >= 0; i--) { mn[i] = mx[i] = ps[i]; if (i < q-1) mn[i] = min(mn[i], mn[i+1]), mx[i] = max(mx[i], mx[i+1]); } int last = -1; for (auto [x, i] : sorted[block]) { while (last < q-1) { ll cur = (last < 0 || v[last] < 0 ? 0 : x); ll cur_ps = last >= 0 ? ps[last] : 0; if (mn[last + 1] - cur_ps + cur < 0 || mx[last + 1] - cur_ps + cur >= x) { int use = last + 1; do { cur += v[use++]; } while (0 < cur && cur < x); last = use - 1; } else break; } ll cur = (last < 0 || v[last] < 0 ? 0 : x); ll cur_ps = last >= 0 ? ps[last] : 0; store[i] = ps.back() - cur_ps + cur; } ll open = 0; ll bound = 0; ll lose = 0; for (int x : v) { open += x; if (open < 0) lose += -open; open = max(open, 0LL); bound = max(bound, open - lose); } int L = block * B, R = min(n - 1, (block + 1) * B - 1); for (int i = L; i <= R; i++) { if (bound >= cap[i]) { a[i] = store[i]; } else { ll free = min((long long) a[i], cap[i] - bound) - lose; a[i] = store[i] + max(0LL, free); } } lzy[block].clear(); } void op(int i, int x) { a[i] += x; a[i] = max(a[i], 0); a[i] = min(a[i], cap[i]); } void upd(int l, int r, int x) { int one = l / B, two = r / B; if (one == two) { push_block(one); for (int i = l; i <= r; i++) { op(i, x); } } else { push_block(one), push_block(two); int one_r = (one + 1) * B - 1; int two_l = two * B; for (int i = l; i <= one_r; i++) { op(i, x); } for (int i = two_l; i <= r; i++) { op(i, x); } for (int i = one + 1; i < two; i++) { lzy[i].push_back(x); } } } vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) { n = sz(c); int q = sz(v); int k = (n + B - 1) / B; for (int i = 0; i < n; i++) cap[i] = c[i]; for (int i = 0; i < n; i++) { sorted[i / B].emplace_back(cap[i], i); } for (int i = 0; i < k; i++) { sort(sorted[i].rbegin(), sorted[i].rend()); } for (int i = 0; i < q; i++) { upd(l[i], r[i], v[i]); /* for (int i = 0; i < n; i++) cerr << a[i] << ' '; cerr << endl; for (int i = 0; i < k; i++) { cerr << "lzy " << i * B << ' ' << (i + 1) * B - 1 << ": "; for (int x : lzy[i]) cerr << x << ' '; cerr << endl; } cerr << endl; */ } for (int i = 0; i < k; i++) push_block(i); return vector<int>(a, a + 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...