Skip to content

Commit

Permalink
Create dynamic.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Jul 12, 2024
1 parent 8d64467 commit 127d974
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions data_structure/segtree/dynamic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//reference: https://justicehui.github.io/medium-algorithm/2020/02/28/DynamicSeg/
struct Node{
Node *l, *r;
ll tsum;
Node(): l(nullptr), r(nullptr), tsum(0) {}
};
void update(Node *node, int s, int e, int p, ll val){
if (s == e){
node->tsum = val; return;
}
int m = (s + e) >> 1;
if (p <= m){
if (!node->l) node->l = new Node();
update(node->l, s, m, p, val);
}
else{
if (!node->r) node->r = new Node();
update(node->r, m + 1, e, p, val);
}
ll left_t = (node->l) ? node->l->tsum : 0;
ll right_t = (node->r) ? node->r->tsum : 0;
node->tsum = left_t + right_t;
}
ll query(Node *node, int s, int e, int le, int ri){
if (!node || ri < s || e < le) return 0;
if (le <= s && e <= ri) return node->tsum;
int m = (s + e) >> 1;
return query(node->l, s, m, le, ri) + query(node->r, m + 1, e, le, ri);
}

0 comments on commit 127d974

Please sign in to comment.