#include "bits/stdc++.h"
#define assert(x)
using namespace std;
using Query = std::tuple<int,int,int>;
std::vector<int> sub2(const std::vector<int>& c, const std::vector<Query>& queries) {
std::vector<long long> diffs((int) c.size() + 1);
for (auto [l, r, v] : queries) {
diffs[l] += v;
diffs[r+1] -= v;
}
std::vector<int> a(c.size());
long long cur = 0;
for (int i = 0; i < (int) a.size(); ++i) {
cur += diffs[i];
a[i] = std::min(cur, (long long) c[i]);
}
return a;
}
// Buffered reader {{{
#pragma once
namespace IO {
const int BUFSIZE = 1<<14;
char buf[BUFSIZE + 1], *inp = buf;
bool reacheof;
char get_char() {
if (!*inp && !reacheof) {
memset(buf, 0, sizeof buf);
int tmp = fread(buf, 1, BUFSIZE, stdin);
if (tmp != BUFSIZE) reacheof = true;
inp = buf;
}
return *inp++;
}
template<typename T>
T get() {
int neg = 0;
T res = 0;
char c = get_char();
while (!std::isdigit(c) && c != '-' && c != '+') c = get_char();
if (c == '+') { neg = 0; }
else if (c == '-') { neg = 1; }
else res = c - '0';
c = get_char();
while (std::isdigit(c)) {
res = res * 10 + (c - '0');
c = get_char();
}
return neg ? -res : res;
}
};
// }}}
// Lazy Segment Tree, copied from AtCoder {{{
// Source: https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp
// Doc: https://atcoder.github.io/ac-library/master/document_en/lazysegtree.html
//
// Notes:
// - Index of elements from 0
// - Range queries are [l, r-1]
// - composition(f, g) should return f(g())
//
// Tested:
// - https://oj.vnoi.info/problem/qmax2
// - https://oj.vnoi.info/problem/lites
// - (range set, add, mult, sum) https://oj.vnoi.info/problem/segtree_itmix
// - (range add (i-L)*A + B, sum) https://oj.vnoi.info/problem/segtree_itladder
// - https://atcoder.jp/contests/practice2/tasks/practice2_l
// - https://judge.yosupo.jp/problem/range_affine_range_sum
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
template<
class S, // node data type
S (*op) (const S&, const S&), // combine 2 nodes
S (*e) (), // identity element
class F, // lazy propagation tag
S (*mapping) (const F&, const S&), // apply tag F on a node
F (*composition) (const F&, const F&), // combine 2 tags
F (*id)() // identity tag
>
struct LazySegTree {
LazySegTree() : LazySegTree(0) {}
explicit LazySegTree(int n) : LazySegTree(vector<S>(n, e())) {}
explicit LazySegTree(const vector<S>& v) : _n((int) v.size()) {
log = ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
lz = std::vector<F>(size, id());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
// 0 <= p < n
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
// 0 <= p < n
S get(int p) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
// Get product in range [l, r-1]
// 0 <= l <= r <= n
// For empty segment (l == r) -> return e()
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return e();
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() {
return d[1];
}
// 0 <= p < n
void apply(int p, F f) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = mapping(f, d[p]);
for (int i = 1; i <= log; i++) update(p >> i);
}
// Apply f on all elements in range [l, r-1]
// 0 <= l <= r <= n
void apply(int l, int r, F f) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return;
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
{
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l2;
r = r2;
}
for (int i = 1; i <= log; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
// Binary search on SegTree to find largest r:
// f(op(a[l] .. a[r-1])) = true (assuming empty array is always true)
// f(op(a[l] .. a[r])) = false (assuming op(..., a[n]), which is out of bound, is always false)
template <bool (*g)(S)> int max_right(int l) {
return max_right(l, [](S x) { return g(x); });
}
template <class G> int max_right(int l, G g) {
assert(0 <= l && l <= _n);
assert(g(e()));
if (l == _n) return _n;
l += size;
for (int i = log; i >= 1; i--) push(l >> i);
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!g(op(sm, d[l]))) {
while (l < size) {
push(l);
l = (2 * l);
if (g(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
// Binary search on SegTree to find smallest l:
// f(op(a[l] .. a[r-1])) = true (assuming empty array is always true)
// f(op(a[l-1] .. a[r-1])) = false (assuming op(a[-1], ..), which is out of bound, is always false)
template <bool (*g)(const S&)> int min_left(int r) {
return min_left(r, [](S x) { return g(x); });
}
template <class G> int min_left(int r, G g) {
assert(0 <= r && r <= _n);
assert(g(e()));
if (r == 0) return 0;
r += size;
for (int i = log; i >= 1; i--) push((r - 1) >> i);
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!g(op(d[r], sm))) {
while (r < size) {
push(r);
r = (2 * r + 1);
if (g(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
vector<S> d;
vector<F> lz;
void update(int k) {
d[k] = op(d[2*k], d[2*k+1]);
}
void all_apply(int k, F f) {
d[k] = mapping(f, d[k]);
if (k < size) lz[k] = composition(f, lz[k]);
}
void push(int k) {
all_apply(2*k, lz[k]);
all_apply(2*k+1, lz[k]);
lz[k] = id();
}
};
// }}}
inline int clamp(int x, int l, int r) {
return (x < l) ? l : (x > r) ? r : x;
}
namespace S3 {
int op(const int& l, [[maybe_unused]] const int& r) { return l; }
int e() { return 0; }
struct F {
int l, r, x;
};
int mapping(const F& f, const int& val) {
return clamp(f.x + val, f.l, f.r);
}
F composition(const F& f, const F& g) {
return F {
clamp(g.l + f.x, f.l, f.r),
clamp(g.r + f.x, f.l, f.r),
f.x + g.x,
};
}
int C;
F id() { return F{0, C, 0}; }
std::vector<int> sub3(const std::vector<int>& c, const std::vector<Query>& queries) {
LazySegTree<int, op, e, F, mapping, composition, id> st(c.size());
C = c[0];
for (auto [l, r, v] : queries) {
st.apply(l, r+1, F{ 0, C, v });
}
std::vector<int> a(c.size());
for (int i = 0; i < (int) a.size(); ++i) {
a[i] = st.get(i);
}
return a;
}
}
std::vector<int> sub4(const std::vector<int>& c, const std::vector<Query>& queries) {
std::vector<long long> sum_v{0};
for (auto [l, r, v] : queries) sum_v.push_back(v);
std::partial_sum(sum_v.begin(), sum_v.end(), sum_v.begin());
std::vector<long long> suffix_min = sum_v, suffix_max = sum_v;
std::partial_sum(suffix_min.rbegin(), suffix_min.rend(), suffix_min.rbegin(),
[] (auto a, auto b) { return std::min(a, b); });
std::partial_sum(suffix_max.rbegin(), suffix_max.rend(), suffix_max.rbegin(),
[] (auto a, auto b) { return std::max(a, b); });
std::vector<int> a(c.size());
for (int i = 0; i < (int) a.size(); ++i) {
// find last index t where suffix_max(t) - suffix_min(t) > c[i]
auto r = std::views::iota(0, (int) sum_v.size());
auto res = std::ranges::partition_point(
r,
[&] (int mid) {
return suffix_max[mid] - suffix_min[mid] > c[i];
});
if (res == r.begin()) a[i] = sum_v.back() - suffix_min[0];
else {
--res;
if (sum_v[*res] < sum_v.back()) {
a[i] = c[i] - (suffix_max[*res] - sum_v.back());
} else {
a[i] = sum_v.back() - suffix_min[*res];
}
}
}
return a;
}
const long long INF = static_cast<long long> (2e18);
struct S { long long suf_min, suf_max; };
S op(const S& left, const S& right) {
return S {
(left.suf_min < right.suf_min) ? left.suf_min : right.suf_min,
(left.suf_max > right.suf_max) ? left.suf_max : right.suf_max,
};
};
S e() { return S{ INF, -INF }; }
S mapping(const long long& f, const S& s) { return S{ f + s.suf_min, f + s.suf_max }; }
long long composition(const long long& f, const long long& g) { return f + g; }
long long id() { return 0; }
long long C; // used for f
bool f(const S& s) { return s.suf_max - s.suf_min <= C; }
std::vector<int> sub5(const std::vector<int>& c, const std::vector<Query>& queries) {
std::vector< std::vector< std::pair<int, int> > > addAt(c.size()), removeAt(c.size());
for (int i = 0; i < (int) queries.size(); ++i) {
auto [l, r, v] = queries[i];
addAt[l].push_back({i, v});
removeAt[r].push_back({i, -v});
}
int q = queries.size();
LazySegTree<S, op, e, long long, mapping, composition, id> st(std::vector<S> (q+1, {0, 0}));
std::vector<int> a(c.size());
for (int i = 0; i < (int) a.size(); ++i) {
for (auto [queryId, val] : addAt[i]) {
st.apply(queryId + 1, q + 1, val);
}
auto suffix_max = [&] (int i) { return st.prod(i, q + 1).suf_max; };
auto suffix_min = [&] (int i) { return st.prod(i, q + 1).suf_min; };
auto sum_v = [&] (int i) { return st.get(i).suf_min; };
// find last index t where suffix_max(t) - suffix_min(t) > c[i]
C = c[i];
int res = st.min_left<f> (q+1);
if (res == 0) a[i] = sum_v(q) - st.all_prod().suf_min;
else {
--res;
auto sum_vq = sum_v(q);
if (sum_v(res) < sum_vq) {
a[i] = c[i] - (suffix_max(res) - sum_vq);
} else {
a[i] = sum_vq - suffix_min(res);
}
}
for (auto [queryId, val] : removeAt[i]) {
st.apply(queryId + 1, q + 1, val);
}
}
return a;
}
std::vector<int> distribute_candies(
std::vector<int> c,
std::vector<int> l,
std::vector<int> r,
std::vector<int> v) {
int n = (int) c.size();
int q = (int) l.size();
std::vector<Query> queries(q);
for (int i = 0; i < q; ++i) {
queries[i] = {l[i], r[i], v[i]};
}
if (*min_element(v.begin(), v.end()) >= 0) return sub2(c, queries);
else if (*min_element(c.begin(), c.end()) == *max_element(c.begin(), c.end())) return S3::sub3(c, queries);
else if (*max_element(l.begin(), l.end()) == 0
&& *min_element(r.begin(), r.end()) == n-1) return sub4(c, queries);
return sub5(c, queries);
}
int main() {
int n = IO::get<int> ();
std::vector<int> c(n);
for (int i = 0; i < n; ++i) c[i] = IO::get<int>();
int q; std::cin >> q;
std::vector<int> l(q), r(q), v(q);
for (int i = 0; i < q; ++i) {
l[i] = IO::get<int> ();
r[i] = IO::get<int> ();
v[i] = IO::get<int> ();
}
auto res = distribute_candies(c, l, r, v);
for (int val : res) std::cout << val << ' ';
std::cout << std::endl;
return 0;
}
Compilation message
candies.cpp:2: warning: "assert" redefined
2 | #define assert(x)
|
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from candies.cpp:1:
/usr/include/assert.h:92: note: this is the location of the previous definition
92 | # define assert(expr) \
|
candies.cpp:23:9: warning: #pragma once in main file
23 | #pragma once
| ^~~~
candies.cpp: In function 'std::vector<int> sub4(const std::vector<int>&, const std::vector<std::tuple<int, int, int> >&)':
candies.cpp:329:23: error: 'std::views' has not been declared
329 | auto r = std::views::iota(0, (int) sum_v.size());
| ^~~~~
candies.cpp:330:25: error: 'std::ranges' has not been declared
330 | auto res = std::ranges::partition_point(
| ^~~~~~