#include "candies.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
using i64 = long long;
struct SegmentTree {
int n;
struct Node {
i64 lazy, mn, mx;
Node() {
lazy = 0;
mn = mx = 0;
}
};
vector<Node> st;
SegmentTree(int n) : n(n) {
st.resize(4 * n);
}
void push(int node) {
st[2 * node + 1].lazy += st[node].lazy;
st[2 * node + 2].lazy += st[node].lazy;
st[2 * node + 1].mn += st[node].lazy;
st[2 * node + 2].mn += st[node].lazy;
st[2 * node + 1].mx += st[node].lazy;
st[2 * node + 2].mx += st[node].lazy;
st[node].lazy = 0;
}
void update(int l, int r, int x) {
update(l, r, x, 0, 0, n - 1);
}
void update(int l, int r, int x, int node, int lx, int rx) {
if (lx > r || rx < l) return;
if (l <= lx && rx <= r) {
st[node].lazy += x;
st[node].mn += x;
st[node].mx += x;
return;
}
push(node);
int mid = (lx + rx) / 2;
update(l, r, x, 2 * node + 1, lx, mid);
update(l, r, x, 2 * node + 2, mid + 1, rx);
st[node].mn = min(st[2 * node + 1].mn, st[2 * node + 2].mn);
st[node].mx = max(st[2 * node + 1].mx, st[2 * node + 2].mx);
}
array<i64, 3> getSuffix(int c) {
return getSuffix(c, 1e18, -1e18, 0, 0, n - 1);
}
array<i64, 3> getSuffix(int c, i64 mn, i64 mx, int node, int lx, int rx) {
if (lx == rx) return {lx, mn, mx};
push(node);
int mid = (lx + rx) / 2;
i64 newMx = max(mx, st[2 * node + 2].mx);
i64 newMn = min(mn, st[2 * node + 2].mn);
if (newMx - newMn > c) {
return getSuffix(c, mn, mx, 2 * node + 2, mid + 1, rx);
} else {
return getSuffix(c, newMn, newMx, 2 * node + 1, lx, mid);
}
}
i64 get(int i) {
return get(i, 0, 0, n - 1);
}
i64 get(int i, int node, int lx, int rx) {
if (lx == rx) return st[node].mn;
push(node);
int mid = (lx + rx) / 2;
if (i <= mid) {
return get(i, 2 * node + 1, lx, mid);
} else {
return get(i, 2 * node + 2, mid + 1, rx);
}
}
};
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<int>> query(n);
for (int i = 0; i < q; ++i) {
query[l[i]].push_back(i);
if (r[i] + 1 < n) {
query[r[i] + 1].push_back(i);
}
}
vector<int> s(n);
SegmentTree st(q + 1);
for (int i = 0; i < n; ++i) {
for (auto j : query[i]) {
if (l[j] == i) {
st.update(j + 1, q, v[j]);
} else {
st.update(j + 1, q, -v[j]);
}
}
i64 ans = st.get(q);
if (st.st[0].mn >= 0 && st.st[0].mx <= c[i]) {
s[i] = ans;
} else {
auto [j, mn, mx] = st.getSuffix(c[i]);
if (st.get(j) < mn) {
s[i] = c[i] - (mx - ans);
} else {
s[i] = ans - mn;
}
}
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
41180 KB |
Output is correct |
2 |
Correct |
307 ms |
40532 KB |
Output is correct |
3 |
Correct |
303 ms |
40272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
174 ms |
29024 KB |
Output is correct |
3 |
Correct |
85 ms |
10024 KB |
Output is correct |
4 |
Correct |
315 ms |
42324 KB |
Output is correct |
5 |
Correct |
342 ms |
42772 KB |
Output is correct |
6 |
Correct |
377 ms |
43256 KB |
Output is correct |
7 |
Correct |
319 ms |
42576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
83 ms |
27336 KB |
Output is correct |
4 |
Correct |
60 ms |
8800 KB |
Output is correct |
5 |
Correct |
170 ms |
35276 KB |
Output is correct |
6 |
Correct |
170 ms |
35876 KB |
Output is correct |
7 |
Correct |
179 ms |
36432 KB |
Output is correct |
8 |
Correct |
161 ms |
35272 KB |
Output is correct |
9 |
Correct |
152 ms |
36776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
329 ms |
41180 KB |
Output is correct |
7 |
Correct |
307 ms |
40532 KB |
Output is correct |
8 |
Correct |
303 ms |
40272 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
174 ms |
29024 KB |
Output is correct |
11 |
Correct |
85 ms |
10024 KB |
Output is correct |
12 |
Correct |
315 ms |
42324 KB |
Output is correct |
13 |
Correct |
342 ms |
42772 KB |
Output is correct |
14 |
Correct |
377 ms |
43256 KB |
Output is correct |
15 |
Correct |
319 ms |
42576 KB |
Output is correct |
16 |
Correct |
0 ms |
436 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
83 ms |
27336 KB |
Output is correct |
19 |
Correct |
60 ms |
8800 KB |
Output is correct |
20 |
Correct |
170 ms |
35276 KB |
Output is correct |
21 |
Correct |
170 ms |
35876 KB |
Output is correct |
22 |
Correct |
179 ms |
36432 KB |
Output is correct |
23 |
Correct |
161 ms |
35272 KB |
Output is correct |
24 |
Correct |
152 ms |
36776 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
58 ms |
9012 KB |
Output is correct |
27 |
Correct |
176 ms |
28752 KB |
Output is correct |
28 |
Correct |
367 ms |
41068 KB |
Output is correct |
29 |
Correct |
316 ms |
41296 KB |
Output is correct |
30 |
Correct |
318 ms |
41660 KB |
Output is correct |
31 |
Correct |
325 ms |
41896 KB |
Output is correct |
32 |
Correct |
345 ms |
41908 KB |
Output is correct |