51 int mid(
int l,
int r) {
52 return l + (r - l) / 2;
62 void update(
int i,
int l,
int r,
int pos, T val) {
63 if(l == r)
t[i] = val;
66 if(pos <= m)
update(i * 2, l, m, pos, val);
67 else update(i * 2 + 1, m + 1, r, pos, val);
68 t[i] =
comb(
t[i * 2],
t[i * 2 + 1]);
81 if(l == tl && r == tr)
return t[i];
84 return comb(
range_comb(i * 2, l, m, tl, std::min(tr, m)),
range_comb(i * 2 + 1, m + 1, r, std::max(tl, m + 1), tr));
123 std::cout <<
"All tests have successfully passed!\n";
class representation of the segment tree
const T ID
Comb(ID, x) = x.
int size
Number of elements available for querying in the tree.
T range_comb(int l, int r)
Returns comb across all values between l and r.
void update(int i, int l, int r, int pos, T val)
Helper method for update method below.
int mid(int l, int r)
Gives the midpoint between two integers.
std::vector< T > t
Vector to represent the tree.
T comb(T x, T y)
Any associative function that combines x and y.
T range_comb(int i, int l, int r, int tl, int tr)
Helper method for range_comb method below.
void update(int pos, T val)
Updates a value at a certain position.
static void test()
Self-test implementations.