#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int mxN = 2e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
pair<pair<int,int>,int> seg[5 * mxN];
int lazy[5 * mxN], n, q, len;
void push(int ind, int l, int r, int cnt = 1) {
seg[ind].f.f += lazy[ind], seg[ind].f.s += lazy[ind];
if (l != r) {
lazy[2 * ind] += lazy[ind];
lazy[2 * ind + 1] += lazy[ind];
if (cnt) {
int mid = (l + r) / 2;
push(2 * ind, l, mid, cnt - 1);
push(2 * ind + 1, mid + 1, r, cnt - 1);
}
}
lazy[ind] = 0;
}
void update(int lo, int hi, int val, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (lo > r || l > hi) {
return;
}
if (lo <= l && r <= hi) {
push(ind, l, r);
lazy[ind] += val;
if (l == r) {
seg[ind].s = l;
}
push(ind, l, r);
return;
}
int mid = (l + r) / 2;
update(lo, hi, val, 2 * ind, l, mid);
push(ind, l, r);
update(lo, hi, val, 2 * ind + 1, mid + 1, r);
push(ind, l, r);
seg[ind].f.f = min(seg[2 * ind].f.f, seg[2 * ind + 1].f.f);
seg[ind].f.s = max(seg[2 * ind].f.s, seg[2 * ind + 1].f.s);
push(ind, l, r);
}
int query(int pos, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (l == r) {
return seg[ind].f.f;
}
int mid = (l + r) / 2;
if (pos <= mid) {
return query(pos, 2 * ind, l, mid);
}
else {
return query(pos, 2 * ind + 1, mid + 1, r);
}
}
int walk(int lo, int val, bool t, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (lo > r || (t && seg[ind].f.s <= val) || (!t && seg[ind].f.f >= val)) {
return (t ? INT_MIN : INT_MAX);
}
if (l == r) {
if (t) {
update(l, len, -seg[ind].f.s + val);
}
else {
update(l, len, -seg[ind].f.f);
}
push(ind, l, r);
return seg[ind].s;
}
int mid = (l + r) / 2;
if (l >= lo) {
push(ind, l, r);
if ((t && seg[2*ind].f.s > val) || (!t && seg[2*ind].f.f < val)) {
return walk(lo, val, t, 2 * ind, l, mid);
}
else {
return walk(lo, val, t, 2 * ind + 1, mid + 1, r);
}
}
push(ind, l, r);
int res = walk(lo, val, t, 2 * ind, l, mid);
push(ind, l, r);
if (res == INT_MIN || res == INT_MAX) {
res = walk(lo, val, t, 2 * ind + 1, mid + 1, r);
push(ind, l, r);
}
return res;
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = c.size(), q = v.size();
int cur = 0;
vector<int> calc;
for (int i = 0; i < q; ++i) {
cur += v[i];
calc.push_back(cur);
if (cur <= 0) {
cur = 0;
calc.clear();
}
}
len = calc.size();
for (int i = 0; i < calc.size(); ++i) {
update(i + 1, i + 1, calc[i]);
}
vector<pair<int,int>> arr;
for (int i = 0; i < n; ++i) {
arr.push_back({c[i], i});
}
sort(arr.begin(), arr.end(), greater<pair<int,int>>());
int ind = 1;
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
while (walk(ind, arr[i].f, 1) != INT_MIN) {
int nex = walk(ind, 0, 0);
if (nex != INT_MAX) {
ind = nex + 1;
}
}
ans[arr[i].s] = query(len);
}
return ans;
}
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for (int i = 0; i < calc.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
617 ms |
10040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
54 ms |
5872 KB |
Output is correct |
4 |
Correct |
288 ms |
4908 KB |
Output is correct |
5 |
Correct |
520 ms |
16384 KB |
Output is correct |
6 |
Correct |
411 ms |
15828 KB |
Output is correct |
7 |
Correct |
539 ms |
13780 KB |
Output is correct |
8 |
Correct |
477 ms |
18356 KB |
Output is correct |
9 |
Correct |
362 ms |
20884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |