-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9ef96a
commit 15c11cb
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//Segtree-General | ||
//General segree, needs node struct (with members def and epsilon(default) for all of them) and operation lambda (merge) | ||
|
||
template <typename T> | ||
class segtree | ||
{ | ||
public: | ||
// 0 based indexing | ||
// def= default value | ||
vector<T> t; | ||
int n; | ||
T def; | ||
function<T(T, T)> merge; | ||
void build(int _n, T _def, function<T(T, T)> _fx) | ||
{ | ||
n = _n; | ||
def = _def; | ||
merge = _fx; | ||
t.assign(n * 2, def); | ||
for (int i = n - 1; i; i--) | ||
t[i] = merge(t[i * 2], t[i * 2 + 1]); | ||
} | ||
void build(vector<T> &a, T _def, function<T(T, T)> _fx) | ||
{ | ||
n = a.size(); | ||
def = _def; | ||
merge = _fx; | ||
t.assign(n * 2, def); | ||
for (int i = 0; i < n; i++) | ||
t[i + n] = T(a[i]); | ||
for (int i = n - 1; i; i--) | ||
t[i] = merge(t[i * 2], t[i * 2 + 1]); | ||
} | ||
void update(int i, T v) | ||
{ | ||
for (t[i += n] = T(v); i;) | ||
{ | ||
i /= 2; | ||
t[i] = merge(t[i * 2], t[i * 2 + 1]); | ||
} | ||
} | ||
// this query is made on [l, r] | ||
T query(int l, int r) | ||
{ | ||
T lans = def, rans = def; | ||
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) | ||
{ | ||
if (l % 2) | ||
lans = merge(lans, t[l++]); | ||
if (r % 2) | ||
rans = merge(t[--r], rans); | ||
} | ||
return merge(lans, rans); | ||
} | ||
}; | ||
|
||
// demo usage | ||
struct node | ||
{ | ||
int val; | ||
node(int x) | ||
{ | ||
val = x; | ||
} | ||
// default value | ||
node() | ||
{ | ||
val = 1e18; | ||
} | ||
}; | ||
|
||
segtree<node> seg; | ||
seg.build(n + 1, node(), [&](node x, node y){ return node(min(x.val, y.val)); }); |