Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Library/Data Structures/SegTreeLazy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ template<typename T> struct SegTree {
vector<T> seg;
vector<T> lazy;
int N;
T NEUTRO = 0;
SegTree(int n) : N(n){ seg.assign(4*N, NEUTRO), lazy.assign(4*N, NEUTRO); }
T IDENTITY = T();
SegTree(int n) : N(n){ seg.assign(4*N, IDENTITY), lazy.assign(4*N, IDENTITY); }
SegTree(vector<T> &lista) : N(lista.size()){
seg.assign(4*N), lazy.assign(4*N, NEUTRO);
seg.assign(4*N, IDENTITY), lazy.assign(4*N, IDENTITY);
build(1, 0, N-1, lista);
}
T join(T lv, T rv){ return lv + rv; }
void unlazy(int no, int l, int r){
if(lazy[no] == NEUTRO) return;
if(lazy[no] == IDENTITY) return;
int m=(l+r)/2, e=no*2, d=e+1;

seg[no] += (r-l+1) * lazy[no]; /// Range Sum

if(l != r) lazy[e] += lazy[no], lazy[d] += lazy[no];
lazy[no] = NEUTRO;
lazy[no] = IDENTITY;
}

T query(int no, int l, int r, int a, int b){
unlazy(no, l, r);
if(b < l || r < a) return NEUTRO;
if(b < l || r < a) return IDENTITY;
if(a <= l && r <= b) return seg[no];
int m=(l+r)/2, e=no*2, d=e+1;

Expand Down Expand Up @@ -70,5 +70,5 @@ Build: O(N)
Query: O(log N) | seg.query(l, r);
Update: O(log N) | seg.update(l, r, v);
Unlazy: O(1)
**Update Join, NEUTRO, Update and Unlazy if needed**
*******************************************************/
**Update Join, IDENTITY, Update and Unlazy if needed**
*******************************************************/