Submission #1014422

#TimeUsernameProblemLanguageResultExecution timeMemory
1014422bleahbleahDistributing Candies (IOI21_candies)C++17
0 / 100
384 ms57700 KiB
#include "candies.h" #include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; //#ifndef DLOCAL // #define cin fin // #define cout fout // ifstream cin(".in"); // ofstream cout(".out"); //#endif using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; template<typename T> struct AINT { vector<T> aint; int n; void init(int n_) { n = n_; aint.assign(n * 2 + 5, T()); } template<class CB> void walk(CB&& cb) { walk(cb, 0, n - 1); } template<class CB> void walk(CB&& cb, int l, int r) { walk(cb, l, r, 1, 0, n - 1); } template<class CB> void walk(CB&& cb, int l, int r, int node, int cl, int cr) { if(cr < l || r < cl) return; if(l <= cl && cr <= r && !cb(aint[node], cl, cr)) return; int mid = (cl + cr) >> 1, L = node + 1, R = node + (cr - cl + 1) * 2; aint[node].push(aint[L], aint[R]); walk(cb, l, r, R, mid + 1, cr); walk(cb, l, r, L, cl, mid); aint[node].pull(aint[L], aint[R]); } }; const ll inf = 1e15 + 5; struct FirstUnit { int mx, rightmn, rightP; int lazy; int sum; FirstUnit(int b = 0, int c = 0, int d = 0, int e = 0): mx(b), rightmn(c), rightP(d), lazy(0), sum(e) {;} void pull(const FirstUnit& a, const FirstUnit& b) { *this = FirstUnit(max(a.mx, b.mx), a.mx >= b.mx? min(a.rightmn, b.rightmn) : b.rightmn, -1, a.sum + b.sum); rightP = b.rightP; if(rightmn == a.rightmn) rightP = a.rightP; return; } void apply(int a) { mx += a; rightmn += a; lazy += a; } void push(FirstUnit& a, FirstUnit& b) { a.apply(lazy); b.apply(lazy); lazy = 0; } }; std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l, std::vector<int> r, std::vector<int> v) { int n = sz(c), q = sz(l); vector<vector<pii>> opers(n + 1); for(int i = 0; i < q; i++) opers[l[i]].emplace_back(i, v[i]), opers[r[i] + 1].emplace_back(i, -v[i]); AINT<FirstUnit> operational; operational.init(q); operational.walk([&](auto& a, int cl, int cr) { if(cl != cr) return 1; a.rightP = cl; return 0; }); vector<int> rez; for(int i = 0; i < n; i++) { for(auto [p, v] : opers[i]) { operational.walk([&](FirstUnit& a, int cl, int cr) { a.apply(v); return 0; }, p, q - 1); operational.walk([&](FirstUnit& a, int cl, int cr) { a.sum += v; return 0; }, p, p); //cerr << p << ' ' << p << '\t' << v << '\n'; } int target_bar = c[i]; int lastinterest = -1, lastmin = 0; operational.walk([&](FirstUnit& a, int cl, int cr) { if(cr <= lastinterest) return 0; int R = a.mx - lastmin, L = a.mx - a.rightmn; if(L > target_bar || L > R) { lastinterest = a.rightP; lastmin = a.rightmn; return 1; } else { return 0; } }); int sum = 0, mx = 0; operational.walk([&](FirstUnit& a, int cl, int cr) { mx = max(mx, a.mx); sum += a.sum; return 0; }, lastinterest + 1, q - 1); rez.emplace_back(sum - max(0, mx - target_bar)); //cerr << lastinterest << ' ' << lastmin << ' ' << sum << '\n'; } return rez; }
#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...