#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<vector<int>> st;
void merge(int node, vector<int> v){
st[node][0] = max(0, min(k, st[node][0]+v[2]));
st[node][1] = max(0, min(k, st[node][1]+v[2]));
st[node][0] = max(st[node][0], v[0]);
st[node][1] = min(st[node][1], v[1]);
st[node][2] = max(-k, min(k, st[node][2]+v[2]));
}
void prop(int node){
merge(node*2, st[node]);
merge(node*2+1, st[node]);
st[node] = {0, k, 0};
}
void update(int l, int r, vector<int> v, int node=1, int tl=0, int tr=n-1){
if (l <= tl && tr <= r){
merge(node, v);
return;
}
prop(node);
int tm = (tl+tr)/2;
if (l <= tm) update(l, r, v, node*2, tl, tm);
if (tm+1 <= r) update(l, r, v, node*2+1, tm+1, tr);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
n = c.size();
k = c[0];
int q = l.size();
st.resize(4*n, {0, k, 0});
vector<int> s(n, 0);
for (int i=0; i<q; i++){
update(l[i], r[i], {max(0, min(k, v[i])), min(k, max(0, k+v[i])), v[i]});
/*int j = 1;
while (j < 4*n){
//cout << st[j][0] << " " << st[j][1] << " " << st[j][2] << " ";
if (!(j & (j+1))) cout << "\n";
j++;
}*/
}
for (int i=0; i<n; i++){
s[i] = 0;
int cur = 1, tl = 0, tr = n-1;
while (tl != tr){
int tm = (tl+tr)/2;
if (i <= tm){
cur = cur*2;
tr = tm;
}
else {
cur = cur*2+1;
tl = tm+1;
}
}
while (cur){
s[i] = max(st[cur][0], min(st[cur][1], s[i]+st[cur][2]));
cur >>= 1;
}
}
//for (int x : s) cout << x << " ";
//cout << "\n";
//}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
847 ms |
53856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Incorrect |
315 ms |
8524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |