#include "candies.h"
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
struct Node {
int p;
int lazy;
int max = INT_MAX;
};
vector<Node> tree;
void lazy_update(int i, int l, int r) {
if (tree[i].lazy != 0) {
if (tree[i].lazy > 0) {
tree[i].p = min(tree[i].max, tree[i].p + tree[i].lazy);
} else {
tree[i].p = max(0, tree[i].p + tree[i].lazy);
}
if (l != r) { // has children
int m = (l + r) / 2;
lazy_update(2 * i, l, m);
lazy_update(2 * i + 1, m + 1, r);
tree[2 * i].lazy += tree[i].lazy;
tree[2 * i + 1].lazy += tree[i].lazy;
}
tree[i].lazy = 0;
}
}
void set_max(int i, int l, int r, int sl, int sr, int max) {
if (l > sr || r < sl) {
return;
}
if (l >= sl && r <= sr) {
tree[i].max = max;
return;
}
int m = (l + r) / 2;
set_max(2 * i, l, m, sl, sr, max);
set_max(2 * i + 1, m + 1, r, sl, sr, max);
}
void update(int i, int l, int r, int sl, int sr, int v) {
lazy_update(i, l, r);
if (l > sr || r < sl) {
return;
}
if (l >= sl && r <= sr) {
tree[i].lazy = v;
lazy_update(i, l, r);
return;
}
int m = (l + r) / 2;
update(2 * i, l, m, sl, sr, v);
update(2 * i + 1, m + 1, r, sl, sr, v);
tree[i].p = tree[2 * i].p + tree[2 * i + 1].p;
}
int query(int i, int l, int r, int sl, int sr) {
lazy_update(i, l, r);
if (l > sr || r < sl) {
return 0;
}
if (l >= sl && r <= sr) {
return tree[i].p;
}
int m = (l + r) / 2;
return query(2 * i, l, m, sl, sr) + query(2 * i + 1, m + 1, r, sl, sr);
}
void final(int i, int l, int r, vector<int>& s) {
lazy_update(i, l, r);
if (l == r) { // is leaf
s[l] = tree[i].p;
return;
}
int m = l + (r - l) / 2;
final(2 * i, l, m, s);
final(2 * i + 1, m + 1, r, s);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
vector<int> s(n);
tree = vector<Node>(4 * n + 5);
for(int i = 0; i < n; ++i) {
set_max(1, 1, n, i + 1, i + 1, c[i]);
}
for (int j = 0; j < q; ++j) {
update(1, 1, n, l[j] + 1, r[j] + 1, v[j]);
}
for(int i = 0; i < n; ++i) {
s[i] = query(1, 1, n, i + 1, i + 1);
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
24 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5058 ms |
18220 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2210 ms |
6052 KB |
Output is correct |
3 |
Correct |
1950 ms |
14004 KB |
Output is correct |
4 |
Execution timed out |
5056 ms |
18148 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Execution timed out |
5061 ms |
5924 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
24 ms |
460 KB |
Output is correct |
6 |
Execution timed out |
5058 ms |
18220 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |