#include <bits/stdc++.h>
#include "candies.h"
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
ll sum[4 * N], mn[4 * N], mx[4 * N];
void Add(int node, int l, int r, int x, int y) {
if (l == r) {
sum[node] += y; mn[node] = mx[node] = sum[node];
return;
}
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
sum[node] = sum[2 * node] + sum[2 * node + 1];
mn[node] = min(mn[2 * node], sum[2 * node] + mn[2 * node + 1]);
mx[node] = max(mx[2 * node], sum[2 * node] + mx[2 * node + 1]);
}
int Solve(int node, int l, int r, int x) {
if (l == r) return max(0ll, min((ll)x, sum[node]));
int mid = l + r >> 1;
if (mx[2 * node + 1] - mn[2 * node + 1] > x) return Solve(2 * node + 1, mid + 1, r, x);
int o = Solve(2 * node, l, mid, x);
if (o + mn[2 * node + 1] < 0) return sum[2 * node + 1] - mn[2 * node + 1];
if (o + mx[2 * node + 1] > x) return x + sum[2 * node + 1] - mx[2 * node + 1];
return o + sum[2 * node + 1];
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size(); int q = v.size();
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < q; i++) {
g[l[i]].push_back({i, v[i]});
if (r[i] < n - 1) g[r[i] + 1].push_back({i, -v[i]});
}
vector<int> ans;
for (int i = 0; i < n; i++) {
for (auto u : g[i]) Add(1, 0, q - 1, u.first, u.second);
ans.push_back(Solve(1, 0, q - 1, c[i]));
}
return ans;
}
Compilation message
candies.cpp: In function 'void Add(int, int, int, int, int)':
candies.cpp:18:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int mid = l + r >> 1;
| ~~^~~
candies.cpp: In function 'int Solve(int, int, int, int)':
candies.cpp:27:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
334 ms |
36548 KB |
Output is correct |
2 |
Correct |
330 ms |
35844 KB |
Output is correct |
3 |
Correct |
331 ms |
35632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
183 ms |
24808 KB |
Output is correct |
3 |
Correct |
60 ms |
10152 KB |
Output is correct |
4 |
Correct |
355 ms |
37632 KB |
Output is correct |
5 |
Correct |
380 ms |
38196 KB |
Output is correct |
6 |
Correct |
406 ms |
38460 KB |
Output is correct |
7 |
Correct |
340 ms |
37800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
119 ms |
21676 KB |
Output is correct |
4 |
Correct |
55 ms |
9080 KB |
Output is correct |
5 |
Correct |
148 ms |
30424 KB |
Output is correct |
6 |
Correct |
167 ms |
31220 KB |
Output is correct |
7 |
Correct |
147 ms |
31708 KB |
Output is correct |
8 |
Correct |
147 ms |
30384 KB |
Output is correct |
9 |
Correct |
153 ms |
31848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
334 ms |
36548 KB |
Output is correct |
7 |
Correct |
330 ms |
35844 KB |
Output is correct |
8 |
Correct |
331 ms |
35632 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
183 ms |
24808 KB |
Output is correct |
11 |
Correct |
60 ms |
10152 KB |
Output is correct |
12 |
Correct |
355 ms |
37632 KB |
Output is correct |
13 |
Correct |
380 ms |
38196 KB |
Output is correct |
14 |
Correct |
406 ms |
38460 KB |
Output is correct |
15 |
Correct |
340 ms |
37800 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
119 ms |
21676 KB |
Output is correct |
19 |
Correct |
55 ms |
9080 KB |
Output is correct |
20 |
Correct |
148 ms |
30424 KB |
Output is correct |
21 |
Correct |
167 ms |
31220 KB |
Output is correct |
22 |
Correct |
147 ms |
31708 KB |
Output is correct |
23 |
Correct |
147 ms |
30384 KB |
Output is correct |
24 |
Correct |
153 ms |
31848 KB |
Output is correct |
25 |
Correct |
1 ms |
312 KB |
Output is correct |
26 |
Correct |
56 ms |
9156 KB |
Output is correct |
27 |
Correct |
218 ms |
24552 KB |
Output is correct |
28 |
Correct |
336 ms |
36280 KB |
Output is correct |
29 |
Correct |
408 ms |
36692 KB |
Output is correct |
30 |
Correct |
321 ms |
36972 KB |
Output is correct |
31 |
Correct |
323 ms |
37052 KB |
Output is correct |
32 |
Correct |
383 ms |
37228 KB |
Output is correct |