#include "candies.h"
#include <algorithm>
#include <limits>
#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();
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 < l.size(); ++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;
}
Compilation message
candies.cpp:12:12: error: 'INT_MAX' was not declared in this scope
12 | int max = INT_MAX;
| ^~~~~~~
candies.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
3 | #include <algorithm>
+++ |+#include <climits>
4 | #include <limits>
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:106:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int j = 0; j < l.size(); ++j) {
| ~~^~~~~~~~~~