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;
struct node {
ll mx, mn, ly;
} tree[808080];
void propagate(int v, int st, int ed) {
tree[v].mx += tree[v].ly;
tree[v].mn += tree[v].ly;
if (st != ed) {
tree[2 * v].ly += tree[v].ly;
tree[2 * v + 1].ly += tree[v].ly;
}
tree[v].ly = 0;
}
void update(int v, int st, int ed, int lt, int rt, int val) {
propagate(v, st, ed);
if (lt > ed || rt < st) return;
if (lt <= st && ed <= rt) {
tree[v].ly += val;
propagate(v, st, ed);
return;
}
int mid = (st + ed) / 2;
update(2 * v, st, mid, lt, rt, val);
update(2 * v + 1, mid + 1, ed, lt, rt, val);
tree[v].mx = max(tree[2 * v].mx, tree[2 * v + 1].mx);
tree[v].mn = min(tree[2 * v].mn, tree[2 * v + 1].mn);
}
node get(int v, int st, int ed, int lt, int rt) {
propagate(v, st, ed);
if (lt > ed || rt < st) return {(ll)-1e18, (ll)+1e18, 0};
if (lt <= st && ed <= rt) return tree[v];
int mid = (st + ed) / 2;
node lt_val = get(2 * v, st, mid, lt, rt);
node rt_val = get(2 * v + 1, mid + 1, ed, lt, rt);
return {max(lt_val.mx, rt_val.mx), min(lt_val.mn, rt_val.mn), 0};
}
std::vector<int> distribute_candies(std::vector<int> C, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = C.size();
int q = l.size();
vector<int> add[202020], rem[202020];
vector<int> ans(n);
for (int i = 0; i < q; i++) {
add[l[i]].push_back(i + 1);
rem[r[i] + 1].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < add[i].size(); j++)
update(1, 0, q, add[i][j], q, v[add[i][j] - 1]);
for (int j = 0; j < rem[i].size(); j++)
update(1, 0, q, rem[i][j], q, -v[rem[i][j] - 1]);
int st = 0, ed = q;
while (st < ed) {
int mid = (st + ed) / 2;
node prev = get(1, 0, q, mid, q);
if (prev.mx - prev.mn <= C[i]) ed = mid;
else st = mid + 1;
}
//cout << st << "\n";
if (st == 0) {
ans[i] = max(0LL, get(1, 0, q, q, q).mx);
continue;
}
if (get(1, 0, q, st, q).mx == get(1, 0, q, st - 1, q).mx) {
ans[i] = C[i] - get(1, 0, q, st, q).mx + get(1, 0, q, q, q).mx;
continue;
}
if (get(1, 0, q, st, q).mn == get(1, 0, q, st - 1, q).mn) {
ans[i] = get(1, 0, q, q, q).mx - get(1, 0, q, st, q).mn;
}
}
return ans;
}
Compilation message (stderr)
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int j = 0; j < add[i].size(); j++)
| ~~^~~~~~~~~~~~~~~
candies.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < rem[i].size(); j++)
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |