# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
804370 | math_rabbit_1028 | Distributing Candies (IOI21_candies) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll c;
struct node {
ll val;
ll add, upp, low;
} tree[808080];
node merger(node lt_val, node rt_val) {
return {lt_val.val + rt_val.val, 0, c, 0};
}
void propagate(ll v, ll st, ll ed) {
tree[v].val = max(min(tree[v].val + tree[v].add, tree[v].upp), tree[v].low);
if (st != ed) {
//c1 = tree[u].upp, d1 = tree[u].add, e1 = tree[u].low
//c2 = tree[v].upp, d2 = tree[v].add, e2 = tree[v].low
for (auto u : {2 * v, 2 * v + 1}) {
tree[u].upp = min(tree[u].upp + tree[v].add, tree[v].upp);
tree[u].add = tree[u].add + tree[v].add;
tree[u].low = max(min(tree[u].low + tree[v].add, tree[v].upp), tree[v].low);
}
}
tree[v].add = 0;
tree[v].upp = c;
tree[v].low = 0;
}
void init(ll v, ll st, ll ed) {
if (st == ed) {
tree[v].val = 0;
tree[v].add = 0;
tree[v].upp = c;
tree[v].low = 0;
return;
}
ll mid = (st + ed) / 2;
init(2 * v, st, mid);
init(2 * v + 1, mid + 1, ed);
tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}
void update(ll v, ll st, ll ed, ll lt, ll rt, ll val) {
propagate(v, st, ed);
if (lt > ed || st > rt) return;
if (lt <= st && ed <= rt) {
tree[v].add += val;
propagate(v, st, ed);
return;
}
ll mid = (st + ed) / 2;
update(2 * v, st, mid, lt, rt, val);
update(2 * v + 1, mid + 1, ed, lt, rt, val);
tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}
ll get(ll v, ll st, ll ed, ll idx) {
propagate(v, st, ed);
if (st > idx || ed < idx) return 0;
if (st == idx && ed == idx) return tree[v].val;
ll mid = (st + ed) / 2;
return get(2 * v, st, mid, idx) + get(2 * v + 1, mid + 1, ed, idx);
}
std::vector<ll> distribute_candies(std::vector<ll> C, std::vector<ll> l,
std::vector<ll> r, std::vector<ll> v) {
ll n = C.size();
ll q = l.size();
std::vector<ll> s(n);
c = C[0];
init(1, 0, n - 1);
for (ll i = 0; i < q; i++) {
update(1, 0, n - 1, l[i], r[i], v[i]);
}
for (ll i = 0; i < n; i++) {
s[i] = get(1, 0, n - 1, i);
}
return s;
}