#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int c;
struct node {
ll val;
ll add, upp, low;
} tree[808080];
node merger(node lt_val, node rt_val) {
return {lt_val.val + rt_val.val, 0, c, 0};
}
void propagate(int v, int st, int ed) {
tree[v].val = max(min(tree[v].val + tree[v].add, tree[v].upp), tree[v].low);
if (st != ed) {
//c1 = tree[u].upp, d1 = tree[u].add, e1 = tree[u].low
//c2 = tree[v].upp, d2 = tree[v].add, e2 = tree[v].low
for (auto u : {2 * v, 2 * v + 1}) {
tree[u].upp = min(tree[u].upp + tree[v].add, tree[v].upp);
tree[u].add = tree[u].add + tree[v].add;
tree[u].low = max(min(tree[u].low + tree[v].add, tree[v].upp), tree[v].low);
}
}
tree[v].add = 0;
tree[v].upp = c;
tree[v].low = 0;
}
void init(int v, int st, int ed) {
if (st == ed) {
tree[v].val = 0;
tree[v].add = 0;
tree[v].upp = c;
tree[v].low = 0;
return;
}
int mid = (st + ed) / 2;
init(2 * v, st, mid);
init(2 * v + 1, mid + 1, ed);
tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}
void update(int v, int st, int ed, int lt, int rt, int val) {
propagate(v, st, ed);
if (lt > ed || st > rt) return;
if (lt <= st && ed <= rt) {
tree[v].add += val;
propagate(v, st, ed);
return;
}
int mid = (st + ed) / 2;
update(2 * v, st, mid, lt, rt, val);
update(2 * v + 1, mid + 1, ed, lt, rt, val);
tree[v] = merger(tree[2 * v], tree[2 * v + 1]);
}
int get(int v, int st, int ed, int idx) {
propagate(v, st, ed);
if (st > idx || ed < idx) return 0;
if (st == idx && ed == idx) return tree[v].val;
int mid = (st + ed) / 2;
return get(2 * v, st, mid, idx) + get(2 * v + 1, mid + 1, ed, idx);
}
std::vector<int> distribute_candies(std::vector<int> C, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = C.size();
int q = l.size();
std::vector<int> s(n);
c = C[0];
init(1, 0, n - 1);
for (int i = 0; i < q; i++) {
update(1, 0, n - 1, l[i], r[i], v[i]);
}
for (int i = 0; i < n; i++) {
s[i] = get(1, 0, n - 1, i);
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
383 ms |
23780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
164 ms |
5156 KB |
Output is correct |
3 |
Correct |
118 ms |
22368 KB |
Output is correct |
4 |
Correct |
403 ms |
29692 KB |
Output is correct |
5 |
Correct |
420 ms |
30084 KB |
Output is correct |
6 |
Correct |
423 ms |
30476 KB |
Output is correct |
7 |
Correct |
393 ms |
29812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |