#include <bits/stdc++.h>
using ll = long long;
#define vi vector<int>
#define FOR(i,a,b) for (int i = a; i <= b; i++)
#define ROF(i,a,b) for (int i = a; i >= b; i--)
using namespace std;
const int MX = 2e5 + 5;
const int BF = 2e3 + 5;
ll pref[MX];
ll mx[MX], mn[MX];
vi distribute_candies(vi c, vi l, vi r, vi v) {
int N = (int) c.size();
int Q = (int) l.size();
if (N <= BF && Q <= BF) {
vi ans(N);
FOR(i, 0, Q - 1) {
FOR(j, l[i], r[i]) {
ans[j] += v[i];
ans[j] = max(ans[j], 0);
ans[j] = min(ans[j], c[j]);
}
}
return ans;
}
if (*min_element(v.begin(), v.end()) > 0) {
FOR(i, 0, Q - 1) {
pref[l[i]] += v[i];
pref[r[i] + 1] -= v[i];
}
vi ans(N);
ll sum = 0;
FOR(i, 0, N - 1) {
sum += pref[i];
ans[i] = min((long long) c[i], sum);
}
return ans;
}
FOR(i,0,Q - 1) {
if (i > 0) pref[i] = pref[i - 1];
pref[i] += v[i];
}
ROF(i,Q - 1,0) {
if (i == Q - 1) {
mx[i] = pref[i];
mn[i] = pref[i];
} else {
mx[i] = max(mx[i + 1], pref[i]);
mn[i] = min(mn[i + 1], pref[i]);
}
}
vi res(N);
FOR(i,0,N - 1) {
if (mx[0] - mn[0] <= c[i]) {
res[i] = pref[Q - 1] - mn[0];
continue;
}
int bot = 0, top = Q - 1, ans = 0;
while (top - bot > 1) {
int mid = bot + top >> 1;
if (mx[mid] - mn[mid] > c[i]) ans = mid, bot = mid;
else top = mid;
}
if (pref[bot] < pref[Q - 1]) res[i] = c[i] - (mx[bot] - pref[Q - 1]);
else res[i] = pref[Q - 1] - mn[bot];
}
return res;
}
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:66:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = bot + top >> 1;
| ~~~~^~~~~
candies.cpp:64:35: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
64 | int bot = 0, top = Q - 1, ans = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
13656 KB |
Output is correct |
2 |
Correct |
117 ms |
12884 KB |
Output is correct |
3 |
Correct |
119 ms |
12720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
69 ms |
12748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
62 ms |
12228 KB |
Output is correct |
4 |
Correct |
65 ms |
3996 KB |
Output is correct |
5 |
Correct |
140 ms |
15576 KB |
Output is correct |
6 |
Correct |
141 ms |
16196 KB |
Output is correct |
7 |
Correct |
141 ms |
16800 KB |
Output is correct |
8 |
Correct |
130 ms |
15428 KB |
Output is correct |
9 |
Correct |
124 ms |
12204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
332 KB |
Output is correct |
6 |
Correct |
147 ms |
13656 KB |
Output is correct |
7 |
Correct |
117 ms |
12884 KB |
Output is correct |
8 |
Correct |
119 ms |
12720 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
69 ms |
12748 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |