#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 1e9+7;
const int B = 450, K = N / B + 10;
int n, a[N], cap[N];
vector<int> lzy[K];
vector<pair<int, int>> sorted[K];
int store[N];
void push_block(int block) {
const auto& v = lzy[block];
int q = sz(v);
if (!q) return;
vector<ll> ps(q);
for (int i = 0; i < q; i++) {
ps[i] = v[i];
if (i) ps[i] += ps[i-1];
}
vector<ll> mn(q), mx(q);
for (int i = q-1; i >= 0; i--) {
mn[i] = mx[i] = ps[i];
if (i < q-1) mn[i] = min(mn[i], mn[i+1]), mx[i] = max(mx[i], mx[i+1]);
}
int last = -1;
for (auto [x, i] : sorted[block]) {
while (last < q-1) {
ll cur = (last < 0 || v[last] < 0 ? 0 : x);
ll cur_ps = last >= 0 ? ps[last] : 0;
if (mn[last + 1] - cur_ps + cur < 0 || mx[last + 1] - cur_ps + cur >= x) {
int use = last + 1;
do {
cur += v[use++];
} while (0 < cur && cur < x);
last = use - 1;
} else break;
}
ll cur = (last < 0 || v[last] < 0 ? 0 : x);
ll cur_ps = last >= 0 ? ps[last] : 0;
store[i] = ps.back() - cur_ps + cur;
}
ll open = 0;
ll bound = 0;
ll lose = 0;
for (int x : v) {
open += x;
if (open < 0) lose += -open;
open = max(open, 0LL);
bound = max(bound, open);
}
int L = block * B, R = min(n - 1, (block + 1) * B - 1);
for (int i = L; i <= R; i++) {
if (bound >= cap[i]) {
a[i] = store[i];
} else {
int free = max(0LL, min((long long) a[i], cap[i] - bound) - lose);
a[i] = store[i] + free;
}
}
lzy[block].clear();
}
void op(int i, int x) {
a[i] += x;
a[i] = max(a[i], 0);
a[i] = min(a[i], cap[i]);
}
void upd(int l, int r, int x) {
int one = l / B, two = r / B;
if (one == two) {
push_block(one);
for (int i = l; i <= r; i++) {
op(i, x);
}
} else {
push_block(one), push_block(two);
int one_r = (one + 1) * B - 1;
int two_l = two * B;
for (int i = l; i <= one_r; i++) {
op(i, x);
}
for (int i = two_l; i <= r; i++) {
op(i, x);
}
for (int i = one + 1; i < two; i++) {
lzy[i].push_back(x);
}
}
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
n = sz(c);
int q = sz(v);
int k = (n + B - 1) / B;
for (int i = 0; i < n; i++) cap[i] = c[i];
for (int i = 0; i < n; i++) {
sorted[i / B].emplace_back(cap[i], i);
}
for (int i = 0; i < k; i++) {
sort(sorted[i].rbegin(), sorted[i].rend());
}
for (int i = 0; i < q; i++) {
upd(l[i], r[i], v[i]);
/*
for (int i = 0; i < n; i++) cerr << a[i] << ' ';
cerr << endl;
for (int i = 0; i < k; i++) {
cerr << "lzy " << i * B << ' ' << (i + 1) * B - 1 << ": ";
for (int x : lzy[i]) cerr << x << ' ';
cerr << endl;
}
cerr << endl;
*/
}
for (int i = 0; i < k; i++) push_block(i);
return vector<int>(a, a + n);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
7 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1573 ms |
14136 KB |
Output is correct |
2 |
Correct |
1918 ms |
13348 KB |
Output is correct |
3 |
Correct |
1649 ms |
13288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
328 ms |
5168 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
289 ms |
12412 KB |
Output is correct |
4 |
Correct |
59 ms |
10732 KB |
Output is correct |
5 |
Correct |
1972 ms |
371660 KB |
Output is correct |
6 |
Correct |
1946 ms |
371580 KB |
Output is correct |
7 |
Correct |
2052 ms |
371632 KB |
Output is correct |
8 |
Correct |
1954 ms |
371728 KB |
Output is correct |
9 |
Correct |
1934 ms |
371548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
7 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |