# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
545319 | 2022-04-04T09:00:49 Z | someone | Distributing Candies (IOI21_candies) | C++17 | 0 ms | 0 KB |
#include "candies.h" #include <bits/stdc++.h> typedef long long ll; using namespace std; struct Node { int deb, fin; ll a, b, x; }; const int M = 1 << 18, N = M << 1; struct Segtree { int cap; Node node[N]; void init(int c) { cap = c; for(int i = 0; i < M; i++) node[i + M].deb = i, node[i + M].fin = i+1; for(int i = M-1; i > 0; i--) node[i].deb = node[i*2].deb, node[i].fin = node[i*2+1].fin; for(int i = M; i < N; i++) node[i].a = node[i].x = 0; for(int i = 0; i < N; i++) node[i].b = cap; } void update(int i, ll x) { i += M; node[i].a = -x, node[i].b = cap - x, node[i].x = x; while(i > 1) { i >>= 1; node[i].x = node[i*2].x + node[i*2+1].x; ll a2 = node[i*2+1].a - node[i*2].x, b2 = node[i*2+1].b - node[i*2].x; node[i].a = max(a2, min(b2, node[i*2].a)); node[i].b = max(a2, min(b2, node[i*2].b)); } } ll getVal(ll cap) { return min(max(0ll, node[1].a), node[1].b) + node[1].x; } }; Segtree tree; vector<int> id[N]; vector<ll> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) { tree.init(c[0]); int n = (int)c.size(), q = (int)l.size(); for(int i = 0; i < q; i++) { id[l[i]].push_back(i); id[r[i]+1].push_back(-i-1); } vector<ll> ans; for(int i = 0; i < n; i++) { for(int j : id[i]) { if(j >= 0) { tree.update(j, v[j]); } else { tree.update(-j-1, 0); } } ans.push_back(tree.getVal(c[i])); } return ans; }