#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] = get(1, 0, q, q, q).mx - get(1, 0, q, 0, q).mn;
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
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++)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
6 ms |
9964 KB |
Output is correct |
5 |
Correct |
11 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1580 ms |
36352 KB |
Output is correct |
2 |
Correct |
1748 ms |
36336 KB |
Output is correct |
3 |
Correct |
1705 ms |
36332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
262 ms |
32676 KB |
Output is correct |
3 |
Correct |
573 ms |
15736 KB |
Output is correct |
4 |
Correct |
1854 ms |
42256 KB |
Output is correct |
5 |
Correct |
1804 ms |
42656 KB |
Output is correct |
6 |
Correct |
1560 ms |
43044 KB |
Output is correct |
7 |
Correct |
1690 ms |
42376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
118 ms |
28456 KB |
Output is correct |
4 |
Correct |
545 ms |
13624 KB |
Output is correct |
5 |
Correct |
1399 ms |
34464 KB |
Output is correct |
6 |
Correct |
1522 ms |
35140 KB |
Output is correct |
7 |
Correct |
1306 ms |
35716 KB |
Output is correct |
8 |
Correct |
1367 ms |
34364 KB |
Output is correct |
9 |
Correct |
1595 ms |
35904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
6 ms |
9964 KB |
Output is correct |
5 |
Correct |
11 ms |
10076 KB |
Output is correct |
6 |
Correct |
1580 ms |
36352 KB |
Output is correct |
7 |
Correct |
1748 ms |
36336 KB |
Output is correct |
8 |
Correct |
1705 ms |
36332 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
262 ms |
32676 KB |
Output is correct |
11 |
Correct |
573 ms |
15736 KB |
Output is correct |
12 |
Correct |
1854 ms |
42256 KB |
Output is correct |
13 |
Correct |
1804 ms |
42656 KB |
Output is correct |
14 |
Correct |
1560 ms |
43044 KB |
Output is correct |
15 |
Correct |
1690 ms |
42376 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
118 ms |
28456 KB |
Output is correct |
19 |
Correct |
545 ms |
13624 KB |
Output is correct |
20 |
Correct |
1399 ms |
34464 KB |
Output is correct |
21 |
Correct |
1522 ms |
35140 KB |
Output is correct |
22 |
Correct |
1306 ms |
35716 KB |
Output is correct |
23 |
Correct |
1367 ms |
34364 KB |
Output is correct |
24 |
Correct |
1595 ms |
35904 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
480 ms |
13620 KB |
Output is correct |
27 |
Correct |
239 ms |
32284 KB |
Output is correct |
28 |
Correct |
1642 ms |
40892 KB |
Output is correct |
29 |
Correct |
1799 ms |
41284 KB |
Output is correct |
30 |
Correct |
1738 ms |
41476 KB |
Output is correct |
31 |
Correct |
1709 ms |
41668 KB |
Output is correct |
32 |
Correct |
1637 ms |
41884 KB |
Output is correct |