#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct res {
long long mn, mx;
};
struct node {
res val;
long long lz;
int st, en;
node *left, *right;
res merge(res l, res r) {
res ret;
ret.mn = min(l.mn, r.mn);
ret.mx = max(l.mx, r.mx);
return ret;
}
void build(int start, int end) {
st = start;
en = end;
if (st == en) {
val.mn = val.mx = 0;
return;
}
int md = (st + en) / 2;
left = new node();
right = new node();
left->build(st, md);
right->build(md + 1, en);
val = merge(left->val, right->val);
}
void lazy() {
left->val.mn += lz;
left->val.mx += lz;
left->lz += lz;
right->val.mn += lz;
right->val.mx += lz;
right->lz += lz;
lz = 0;
}
res query(int lf, int rg) {
if (lf <= st && en <= rg) return val;
lazy();
if (rg <= left->en) return left->query(lf, rg);
if (lf >= right->st) return right->query(lf, rg);
return merge(left->query(lf, rg), right->query(lf, rg));
}
void update(int lf, int rg, int x) {
if (st > rg || en < lf) return;
if (lf <= st && en <= rg) {
val.mn += x;
val.mx += x;
lz += x;
return;
}
lazy();
left->update(lf, rg, x);
right->update(lf, rg, x);
val = merge(left->val, right->val);
}
} sg;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size(), q = (int)l.size();
vector<vector<pair<int, int>>> qr(n + 1);
for (int i = 0; i < q; i++) {
qr[l[i]].push_back({i + 1, v[i]});
qr[r[i] + 1].push_back({i + 1, -v[i]});
}
vector<int> ans(n);
sg.build(0, q);
for (int i = 0; i < n; i++) {
for (auto [j, x] : qr[i]) {
sg.update(j, q, x);
}
auto [mn, mx] = sg.query(0, q);
long long val = sg.query(q, q).mn;
if (mx - mn <= c[i]) {
ans[i] = val - mn;
continue;
}
int lf = 0, rg = q, pos = -1;
while (lf <= rg) {
int md = (lf + rg) / 2;
auto [cmn, cmx] = sg.query(md, q);
if (cmx - cmn > c[i]) {
pos = md;
lf = md + 1;
} else {
rg = md - 1;
}
}
long long nw = sg.query(pos, pos).mn;
auto [fmn, fmx] = sg.query(pos, q);
if (nw == fmn) {
ans[i] = c[i] - (fmx - val);
} else if (nw == fmx) {
ans[i] = val - fmn;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
732 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
719 ms |
49104 KB |
Output is correct |
2 |
Correct |
686 ms |
48208 KB |
Output is correct |
3 |
Correct |
634 ms |
47952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
202 ms |
37336 KB |
Output is correct |
3 |
Correct |
151 ms |
10032 KB |
Output is correct |
4 |
Correct |
643 ms |
50168 KB |
Output is correct |
5 |
Correct |
617 ms |
50772 KB |
Output is correct |
6 |
Correct |
499 ms |
50772 KB |
Output is correct |
7 |
Correct |
564 ms |
50108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
92 ms |
36024 KB |
Output is correct |
4 |
Correct |
90 ms |
8792 KB |
Output is correct |
5 |
Correct |
279 ms |
43704 KB |
Output is correct |
6 |
Correct |
323 ms |
44468 KB |
Output is correct |
7 |
Correct |
313 ms |
44980 KB |
Output is correct |
8 |
Correct |
280 ms |
43720 KB |
Output is correct |
9 |
Correct |
374 ms |
45120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
732 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
6 |
Correct |
719 ms |
49104 KB |
Output is correct |
7 |
Correct |
686 ms |
48208 KB |
Output is correct |
8 |
Correct |
634 ms |
47952 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
202 ms |
37336 KB |
Output is correct |
11 |
Correct |
151 ms |
10032 KB |
Output is correct |
12 |
Correct |
643 ms |
50168 KB |
Output is correct |
13 |
Correct |
617 ms |
50772 KB |
Output is correct |
14 |
Correct |
499 ms |
50772 KB |
Output is correct |
15 |
Correct |
564 ms |
50108 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
92 ms |
36024 KB |
Output is correct |
19 |
Correct |
90 ms |
8792 KB |
Output is correct |
20 |
Correct |
279 ms |
43704 KB |
Output is correct |
21 |
Correct |
323 ms |
44468 KB |
Output is correct |
22 |
Correct |
313 ms |
44980 KB |
Output is correct |
23 |
Correct |
280 ms |
43720 KB |
Output is correct |
24 |
Correct |
374 ms |
45120 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
87 ms |
9020 KB |
Output is correct |
27 |
Correct |
175 ms |
36920 KB |
Output is correct |
28 |
Correct |
437 ms |
48724 KB |
Output is correct |
29 |
Correct |
565 ms |
48980 KB |
Output is correct |
30 |
Correct |
614 ms |
49392 KB |
Output is correct |
31 |
Correct |
539 ms |
49584 KB |
Output is correct |
32 |
Correct |
551 ms |
49632 KB |
Output is correct |