It is the data structure for monoids $(S, \cdot: S \times S \to S, e \in S)$, i.e., the algebraic structure that satisfies the following properties.
Given an array $S$ of length $N$, it processes the following queries in $O(\log N)$ time (see Appendix for further details).
For simplicity, in this document, we assume that the oracles op
and e
work in constant time. If these oracles work in $O(T)$ time, each time complexity appear in this document is multipled by $O(T)$.
(1) segtree<S, op, e> seg(int n)
(2) segtree<S, op, e> seg(vector<S> v)
The following should be defined.
S
S op(S a, S b)
S e()
For example, for Range Minimum Query, the definitions are as follows.
int op(int a, int b) {
return min(a, b);
}
int e() {
return (int)(1e9);
}
segtree<int, op, e> seg(10);
a
of length n
. All the elements are initialized to e()
.a
of length n = v.size()
, initialized to v
.See examples and here for further details.
Constraints
Complexity
void seg.set(int p, S x)
It assigns $x$ to a[p]
.
Constraints
Complexity
S seg.get(int p)
It returns a[p]
.
Constraints
Complexity
S seg.prod(int l, int r)
It returns op(a[l], ..., a[r - 1])
, assuming the properties of the monoid. It returns e()
if $l = r$.
Constraints
Complexity
S seg.all_prod()
It returns op(a[0], ..., a[n - 1])
, assuming the properties of the monoid. It returns e()
if $n = 0$.
Complexity
(1) int seg.max_right<f>(int l)
(2) int seg.max_right<F>(int l, F f)
bool f(S x)
should be defined. S
as the argument and returns bool
should be defined. It returns an index r
that satisfies both of the following.
r = l
or f(op(a[l], a[l + 1], ..., a[r - 1])) = true
r = n
or f(op(a[l], a[l + 1], ..., a[r])) = false
If f
is monotone, this is the maximum r
that satisfies f(op(a[l], a[l + 1], ..., a[r - 1])) = true
.
Constraints
f
is called with the same argument, it returns the same value, i.e., f
has no side effect.f(e()) = true
Complexity
(1) int seg.min_left<f>(int r)
(2) int seg.min_left<F>(int r, F f)
bool f(S x)
should be defined. S
as the argument and returns bool
should be defined. It returns an index l
that satisfies both of the following.
l = r
or f(op(a[l], a[l + 1], ..., a[r - 1])) = true
l = 0
or f(op(a[l - 1], a[l], ..., a[r - 1])) = false
If f
is monotone, this is the minimum l
that satisfies f(op(a[l], a[l + 1], ..., a[r - 1])) = true
.
Constraints
f
is called with the same argument, it returns the same value, i.e., f
has no side effect.f(e()) = true
Complexity