Skip to content

Commit

Permalink
Create nonrecursive.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Sep 5, 2024
1 parent 33d7d7d commit 731b59c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions data_structure/segtree/nonrecursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
template<typename T>
struct segtree{
int sz, n;
vector<T> seg;
segtree(): sz(0) {}
segtree(int sz): sz(sz){seg.resize(2 * sz, 0LL);}
T& operator[](int idx){return seg[idx];}
void init(int n){
for (int i = 0; i < n; i++) cin >> seg[sz + i];
for (int i = sz - 1; i > 0; i--) seg[i] = seg[i << 1] + seg[i << 1 | 1];
}
void update(int pos, T val){
seg[sz + pos] = val;
for (pos += sz; pos > 1; pos >>= 1){
seg[pos >> 1] = seg[pos] + seg[pos ^ 1];
}
}
T query(int l, int r){
T ans = 0;
for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1){
if (l&1) ans += seg[l++];
if (~r&1) ans += seg[r--];
}
return ans;
}
};

0 comments on commit 731b59c

Please sign in to comment.