Skip to content

Commit

Permalink
Improve segment_tree (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 authored Oct 8, 2023
1 parent e952b16 commit 2151a1b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cpp/structures/segment_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ 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 +57,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(v[l]);
return;
}
int m = (l + r) >> 1;
Expand Down

0 comments on commit 2151a1b

Please sign in to comment.