#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct SegmentTree{
struct node{
long long Min, Max; // min-max suffix
long long lazy;
node(): Min(0), Max(0), lazy(0) {};
node(long long x): Min(x), Max(x), lazy(0) {};
};
void combine(node &par, const node &lc, const node &rc){
par.Min = min(lc.Min, rc.Min);
par.Max = max(lc.Max, rc.Max);
}
node neutral_element;
int n;
vector<node> tree;
SegmentTree(int _n){
n = _n;
tree.assign(4 * n + 5, node());
neutral_element.Min = (long long)1e16;
neutral_element.Max = (long long)-1e16;
}
void upd(int v, long long lazy){
tree[v].Min += lazy;
tree[v].Max += lazy;
tree[v].lazy += lazy;
}
void push(int v, int tl, int tr){
if (tl == tr || tree[v].lazy == 0) return;
upd(v << 1, tree[v].lazy);
upd(v << 1 | 1, tree[v].lazy);
tree[v].lazy = 0;
}
void update(int v, int tl, int tr, int l, int r, long long val){
push(v, tl, tr);
if (l <= tl && tr <= r){
upd(v, val);
return;
}
if (tl > r || tr < l) return;
int tm = (tl + tr) >> 1;
update(v << 1, tl, tm, l, r, val);
update(v << 1 | 1, tm + 1, tr, l, r, val);
combine(tree[v], tree[v << 1], tree[v << 1 | 1]);
}
void update(int l, int r, long long val){
update(1, 0, n - 1, l, r, val);
}
node get(int v, int tl, int tr, int l, int r){
push(v, tl, tr);
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return neutral_element;
int tm = (tl + tr) >> 1;
node res;
combine(res, get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
return res;
}
long long get_value(int pos){
return get(1, 0, n - 1, pos, pos).Max;
}
long long get_diff(int l, int r){
node res = get(1, 0, n - 1, l, r);
return res.Max - res.Min;
}
};
vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) {
int n = (int)C.size(), q = (int)V.size();
V.insert(V.begin(), (int) -2e9), L.insert(L.begin(), 0), R.insert(R.begin(), n - 1);
V.insert(V.begin(), (int) 2e9), L.insert(L.begin(), 0), R.insert(R.begin(), n - 1);
q += 2;
vector<vector<int>> opening(n), closing(n);
for (int i = 0; i < q; i++){
opening[L[i]].push_back(i);
closing[R[i]].push_back(i);
}
vector<int> ans(n, 0);
SegmentTree st(q);
long long sum = 0;
for (int i = 0; i < n; i++){
for (int j : opening[i]){
st.update(j, q - 1, V[j]);
sum += V[j];
}
int j = -1;
int l = 0, r = q - 1;
while (l <= r){
int m = (l + r) >> 1;
if (st.get_diff(m, q - 1) >= C[i]){
j = m;
l = m + 1;
} else {
r = m - 1;
}
}
assert(j != -1);
long long diff = st.get_diff(j, q - 1) - C[i];
long long Y = st.get_value(j);
if (V[j] > 0){ /// full
Y -= diff;
ans[i] = C[i] + sum - Y;
} else { /// empty
Y += diff;
ans[i] = sum - Y;
}
for (int j : closing[i]){
st.update(j, q - 1, -V[j]);
sum -= V[j];
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1190 ms |
44816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |