#include<bits/stdc++.h>
namespace internal {
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
}
template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct lazy_segtree {
public:
lazy_segtree() : lazy_segtree(0) {}
explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
explicit lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
size = (int)internal::bit_ceil((unsigned int)(_n));
log = internal::countr_zero((unsigned int)size);
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);
}
}
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);
}
S get(int p) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
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]; }
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);
}
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);
}
}
private:
int _n, size, log;
std::vector<S> d;
std::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();
}
};
int64_t add(int64_t a,int64_t b){
return a+b;
}
int64_t e(){
return 0;
}
struct F{
int64_t x;
};
int64_t mapping(F f,int64_t s){
return f.x+s;
}
F composition(F a,F b){
return F{a.x+b.x};
}
F id(){
return F{(int64_t)0};
}
signed main(){
uint32_t N = 8;
std::vector<int64_t> arr = {3, 2 ,4 ,5 ,1 ,1 ,5 ,3};
lazy_segtree<int64_t,add,e,F,mapping,composition,id> tree(arr);
tree.apply(1,5,F{1});
for(uint32_t i=0;i<N;i++){
std::cout << tree.get(i) << " ";
}
std::cout <<"\n";
std::cout << tree.prod(1,5) << "\n";
}
It is not a bug in their implementation. When you apply a range increment of $$$x$$$ to a segment, that segment's sum should increase by the segment's length multiplied by $$$x$$$, whereas your "mapping" function just increases said sum by $$$x$$$.
You must maintain the length of the segment in the node of segtree to execute Range Sum & Range Add Queries.
Your mapping should be {s,length} -> {s+f.x*length,length} instead of {s} -> {s+f.x}, because mapping should update all of the values in the range simultaneously.
Here is an example implementation.
Ahh I get your point but , How is it possible that when I print individual elements they are an exact same which is intended but with the prod function it seems to fail ? It’s super confusing
Lazy propagation itself is correct so each value for $$$[i,i+1)$$$ (single element) are collect if you access them, but $$$[i,j) (j \neq i+1)$$$ are incollect so the answers of range queries are wrong.
Ok I see, the examples link you have shared helps, Thanks physics0523
Auto comment: topic has been updated by bhatKa (previous revision, new revision, compare).