#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
const int mxN = 2e5 + 1;
struct info {
int net; // net change
int mini, maxi;
info(): net(0), mini(1e18), maxi(-1e18) {};
info(int val): net(val), mini(val), maxi(val) {};
};
info merge(info &lhs, info &rhs) {
info res;
res.net = lhs.net + rhs.net;
res.mini = min(lhs.mini, lhs.net + rhs.mini);
res.maxi = max(lhs.maxi, lhs.net + rhs.maxi);
return res;
}
info EMPTY = info();
info ZERO = info(0);
info seg[4 * mxN];
int lazy[4 * mxN];
int n, q;
void build(int l = 0, int r = q, int idx = 1) {
if (l == r) {
if (l == 0) seg[idx] = info(0);
else seg[idx] = info();
return;
}
int mid = (l + r) >> 1;
build(l, mid, (idx << 1));
build(mid + 1, r, (idx << 1) | 1);
seg[idx] = merge(seg[(idx << 1)], seg[(idx << 1) | 1]);
}
void update(int pos, int val, int l = 0, int r = q, int idx = 1) {
if (l == r) {
if (val == 0) seg[idx] = info();
else seg[idx] = info(val);
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(pos, val, l, mid, (idx << 1));
else update(pos, val, mid + 1, r, (idx << 1) | 1);
seg[idx] = merge(seg[(idx << 1)], seg[(idx << 1) | 1]);
}
// range find net min/max
pii query1(int tl, int tr, int l = 0, int r = q, int idx = 1, int buff = 0) {
if (tl > tr) return {0, 0};
if (tl <= l && r <= tr) return {buff + seg[idx].mini, buff + seg[idx].maxi};
int mid = (l + r) >> 1;
pii res = {1e18, -1e18}, ret;
if (tl <= mid) {
ret = query1(tl, tr, l, mid, (idx << 1), buff);
res.ff = min(res.ff, ret.ff); res.ss = max(res.ss, ret.ss);
}
if (tr > mid) {
ret = query1(tl, tr, mid + 1, r, (idx << 1) | 1, buff + seg[(idx << 1)].net);
res.ff = min(res.ff, ret.ff); res.ss = max(res.ss, ret.ss);
}
return res;
}
// find largest pos s.t. max(upd[pos..n]) - min(upd[pos..n]) > (tar = c[i])
int query2(int tar, pii range, int l = 0, int r = q, int idx = 1, int buff = 0) {
if (max(range.ss, buff + seg[idx].maxi) - min(range.ff, buff + seg[idx].mini) <= tar) {
return -1;
}
if (l == r) return l;
int mid = (l + r) >> 1;
int rmini = buff + seg[(idx << 1)].net + seg[(idx << 1) | 1].mini;
int rmaxi = buff + seg[(idx << 1)].net + seg[(idx << 1) | 1].maxi;
if (max(range.ss, rmaxi) - min(range.ff, rmini) <= tar) {
range.ss = max(range.ss, rmaxi);
range.ff = min(range.ff, rmini);
return query2(tar, range, l, mid, (idx << 1), buff);
} else {
return query2(tar, range, mid + 1, r, (idx << 1) | 1, buff + seg[(idx << 1)].net);
}
return -1;
}
vector<int32_t> distribute_candies(vector<int32_t> c, vector<int32_t> l,
vector<int32_t> r, vector<int32_t> v) {
n = c.size(); q = v.size();
build();
vector<pii> upd[n + 2];
for (int i = 0; i < q; i++) {
l[i]++; r[i]++;
upd[l[i]].pb({i + 1, v[i]});
upd[r[i] + 1].pb({i + 1, 0});
}
vector<int32_t> ans;
for (int i = 1; i <= n; i++) {
for (pii &cur : upd[i]) {
update(cur.ff, cur.ss);
}
int tot = seg[1].net;
int crit = query2(c[i - 1], {tot, tot});
pii range;
if (crit == -1) {
range = query1(0, q);
ans.pb(tot - range.ff);
continue;
}
int prev = query1(crit, crit).ff;
range = query1(crit + 1, q);
if (prev < range.ff) {
range.ff = range.ss - c[i - 1];
ans.pb(tot - range.ff);
} else {
range.ss = range.ff + c[i - 1];
ans.pb(c[i - 1] - (range.ss - tot));
}
}
return ans;
}
#undef int
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19156 KB |
Output is correct |
4 |
Correct |
9 ms |
19156 KB |
Output is correct |
5 |
Correct |
10 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
323 ms |
41728 KB |
Output is correct |
2 |
Correct |
297 ms |
41668 KB |
Output is correct |
3 |
Correct |
303 ms |
41676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19152 KB |
Output is correct |
2 |
Correct |
181 ms |
32712 KB |
Output is correct |
3 |
Correct |
74 ms |
27692 KB |
Output is correct |
4 |
Correct |
324 ms |
41672 KB |
Output is correct |
5 |
Correct |
348 ms |
41696 KB |
Output is correct |
6 |
Correct |
337 ms |
41600 KB |
Output is correct |
7 |
Correct |
292 ms |
41668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
89 ms |
30224 KB |
Output is correct |
4 |
Correct |
67 ms |
26592 KB |
Output is correct |
5 |
Correct |
158 ms |
38044 KB |
Output is correct |
6 |
Correct |
170 ms |
38068 KB |
Output is correct |
7 |
Correct |
173 ms |
38144 KB |
Output is correct |
8 |
Correct |
153 ms |
38076 KB |
Output is correct |
9 |
Correct |
174 ms |
38180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19156 KB |
Output is correct |
4 |
Correct |
9 ms |
19156 KB |
Output is correct |
5 |
Correct |
10 ms |
19284 KB |
Output is correct |
6 |
Correct |
323 ms |
41728 KB |
Output is correct |
7 |
Correct |
297 ms |
41668 KB |
Output is correct |
8 |
Correct |
303 ms |
41676 KB |
Output is correct |
9 |
Correct |
8 ms |
19152 KB |
Output is correct |
10 |
Correct |
181 ms |
32712 KB |
Output is correct |
11 |
Correct |
74 ms |
27692 KB |
Output is correct |
12 |
Correct |
324 ms |
41672 KB |
Output is correct |
13 |
Correct |
348 ms |
41696 KB |
Output is correct |
14 |
Correct |
337 ms |
41600 KB |
Output is correct |
15 |
Correct |
292 ms |
41668 KB |
Output is correct |
16 |
Correct |
9 ms |
19028 KB |
Output is correct |
17 |
Correct |
8 ms |
19028 KB |
Output is correct |
18 |
Correct |
89 ms |
30224 KB |
Output is correct |
19 |
Correct |
67 ms |
26592 KB |
Output is correct |
20 |
Correct |
158 ms |
38044 KB |
Output is correct |
21 |
Correct |
170 ms |
38068 KB |
Output is correct |
22 |
Correct |
173 ms |
38144 KB |
Output is correct |
23 |
Correct |
153 ms |
38076 KB |
Output is correct |
24 |
Correct |
174 ms |
38180 KB |
Output is correct |
25 |
Correct |
9 ms |
19028 KB |
Output is correct |
26 |
Correct |
63 ms |
26564 KB |
Output is correct |
27 |
Correct |
182 ms |
32716 KB |
Output is correct |
28 |
Correct |
310 ms |
41660 KB |
Output is correct |
29 |
Correct |
330 ms |
41668 KB |
Output is correct |
30 |
Correct |
335 ms |
41652 KB |
Output is correct |
31 |
Correct |
333 ms |
41668 KB |
Output is correct |
32 |
Correct |
342 ms |
41668 KB |
Output is correct |