Skip to content

Commit

Permalink
Improve segment_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Oct 8, 2023
1 parent e952b16 commit abf464e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cpp/structures/segment_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ struct segtree {
long long sum = 0;
long long add = 0;

// set initial value for a leave
void initialize(long long v) {
mx = v;
}

// apply aggregate operation to the node
void apply(int l, int r, long long v) {
mx += v;
sum += (r - l + 1) * v;
add += v;
}
};

// construct a node from its children
static node unite(const node &a, const node &b) {
node res;
res.mx = max(a.mx, b.mx);
Expand Down Expand Up @@ -52,7 +59,7 @@ struct segtree {
template <class T>
void build(int x, int l, int r, const vector<T> &v) {
if (l == r) {
tree[x].apply(l, r, v[l]);
tree[x].initialize([l]);
return;
}
int m = (l + r) >> 1;
Expand Down

0 comments on commit abf464e

Please sign in to comment.