#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct node {
long long mn, mx, ps;
};
int n, q, com, p;
long long mn, mx, all;
vector<node> st;
void ins(int x, int l, int r, int ll, int rr, int w) {
if (l > rr || ll > r) {
return;
}
if (ll <= l && r <= rr) {
st[x].mx += w;
st[x].mn += w;
st[x].ps += w;
return;
}
int ls = x << 1;
ins(ls, l, (l + r) / 2, ll, rr, w);
ins(ls ^ 1, (l + r) / 2 + 1, r, ll, rr, w);
st[x].mx = max(st[ls].mx, st[ls ^ 1].mx) + st[x].ps;
st[x].mn = min(st[ls].mn, st[ls ^ 1].mn) + st[x].ps;
}
void suf(int x, int w, long long add) {
if (x >= com) {
mn = min(mn, st[x].mn + add);
mx = max(mx, st[x].mx + add);
p = mn == (st[x].mn + add);
return;
}
add += st[x].ps;
int ls = x << 1;
if (max(mx, st[ls ^ 1].mx + add) - min(mn, st[ls ^ 1].mn + add) >= w) { // idziemy w prawo
suf(ls ^ 1, w, add);
}
else {
mx = max(mx, st[ls ^ 1].mx + add);
mn = min(mn, st[ls ^ 1].mn + add);
suf(ls, w, add);
}
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
n = c.size();
q = l.size();
com = 1;
while (com < q + 1) {
com <<= 1;
}
st.resize(com << 1);
vector<pair<int, int>> z[n + 1];
for (int i = 0; i < q; ++i) {
z[l[i]].push_back({i + 1, v[i]});
z[r[i] + 1].push_back({i + 1, -v[i]});
}
vector<int> odp(n);
for (int i = 0; i < n; ++i) {
for (auto j : z[i]) {
all += j.second;
ins(1, 0, com - 1, j.first, com, j.second);
}
if (st[1].mx - st[1].mn <= c[i]) {
odp[i] = all - st[1].mn;
continue;
}
mn = 1e18;
mx = -1e18;
p = 0;
suf(1, c[i], 0);
//~ printf("I = %d | %lld | %d | %lld %lld\n", i, all, p, mn, mx);
if (p) {
odp[i] = all + c[i] - mx;
}
else {
odp[i] = all - mn;
}
}
return odp;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
341 ms |
36240 KB |
Output is correct |
2 |
Correct |
343 ms |
35448 KB |
Output is correct |
3 |
Correct |
355 ms |
35408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
220 ms |
24772 KB |
Output is correct |
3 |
Correct |
57 ms |
10928 KB |
Output is correct |
4 |
Correct |
323 ms |
37292 KB |
Output is correct |
5 |
Correct |
334 ms |
37824 KB |
Output is correct |
6 |
Correct |
320 ms |
38100 KB |
Output is correct |
7 |
Correct |
309 ms |
37440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
100 ms |
23280 KB |
Output is correct |
4 |
Correct |
54 ms |
8808 KB |
Output is correct |
5 |
Correct |
157 ms |
31012 KB |
Output is correct |
6 |
Correct |
173 ms |
31692 KB |
Output is correct |
7 |
Correct |
162 ms |
32168 KB |
Output is correct |
8 |
Correct |
151 ms |
30904 KB |
Output is correct |
9 |
Correct |
160 ms |
32400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
592 KB |
Output is correct |
6 |
Correct |
341 ms |
36240 KB |
Output is correct |
7 |
Correct |
343 ms |
35448 KB |
Output is correct |
8 |
Correct |
355 ms |
35408 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
220 ms |
24772 KB |
Output is correct |
11 |
Correct |
57 ms |
10928 KB |
Output is correct |
12 |
Correct |
323 ms |
37292 KB |
Output is correct |
13 |
Correct |
334 ms |
37824 KB |
Output is correct |
14 |
Correct |
320 ms |
38100 KB |
Output is correct |
15 |
Correct |
309 ms |
37440 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
288 KB |
Output is correct |
18 |
Correct |
100 ms |
23280 KB |
Output is correct |
19 |
Correct |
54 ms |
8808 KB |
Output is correct |
20 |
Correct |
157 ms |
31012 KB |
Output is correct |
21 |
Correct |
173 ms |
31692 KB |
Output is correct |
22 |
Correct |
162 ms |
32168 KB |
Output is correct |
23 |
Correct |
151 ms |
30904 KB |
Output is correct |
24 |
Correct |
160 ms |
32400 KB |
Output is correct |
25 |
Correct |
1 ms |
288 KB |
Output is correct |
26 |
Correct |
51 ms |
8772 KB |
Output is correct |
27 |
Correct |
190 ms |
24396 KB |
Output is correct |
28 |
Correct |
303 ms |
36084 KB |
Output is correct |
29 |
Correct |
338 ms |
36316 KB |
Output is correct |
30 |
Correct |
335 ms |
36648 KB |
Output is correct |
31 |
Correct |
334 ms |
36780 KB |
Output is correct |
32 |
Correct |
338 ms |
36904 KB |
Output is correct |