Lazy Segtree
It is the data structure for the pair of a monoid $(S, \cdot: S \times S \to S, e \in S)$ and a set $F$ of $S \to S$ mappings that satisfies the following properties.
- $F$ contains the identity map $\mathrm{id}$, where the identity map is the map that satisfies $\mathrm{id}(x) = x$ for all $x \in S$.
- $F$ is closed under composition, i.e., $f \circ g \in F$ holds for all $f, g \in F$.
- $f(x \cdot y) = f(x) \cdot f(y)$ holds for all $f \in F$ and $x, y \in S$.
Given an array $S$ of length $N$, it processes the following queries in $O(\log N)$ time (see Appendix for further details).
- Acting the map $f\in F$ (cf. $x = f(x)$) on all the elements of an interval
- Calculating the product of the elements of an interval
For simplicity, in this document, we assume that the oracles op, e, mapping, composition, and id work in constant time. If these oracles work in $O(T)$ time, each time complexity appear in this document is multipled by $O(T)$.
Constructor
(1) lazy_segtree<S, op, e, F, mapping, composition, id> seg(int n);
(2) lazy_segtree<S, op, e, F, mapping, composition, id> seg(vector<T> v);
The following should be defined.
- The type
Sof the monoid - The binary operation
S op(S a, S b) - The function
S e()that returns $e$ - The type
Fof the map - The function
S mapping(F f, S x)that returns $f(x)$ - The function
F composition(F f, F g)that returns $f \circ g$ - The function
F id()that returns $\mathrm{id}$
See examples and here for further details.
- (1): It creates an array
aof lengthn. All the elements are initialized toe(). - (2): It creates an array
aof lengthn = v.size(), initialized tov.
Constraints
- $0 \leq n \leq 10^8$
Complexity
- $O(n)$
set
void seg.set(int p, S x)
a[p] = x
Constraints
- $0 \leq p < n$
Complexity
- $O(\log n)$
get
S seg.get(int p)
It returns a[p].
Constraints
- $0 \leq p < n$
Complexity
- $O(\log n)$
prod
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
- $0 \leq l \leq r \leq n$
Complexity
- $O(\log n)$
all_prod
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
- $O(1)$
apply
(1) void seg.apply(int p, F f)
(2) void seg.apply(int l, int r, F f)
- (1) It applies
a[p] = f(a[p]). - (2) It applies
a[i] = f(a[i])for alli = l..r-1.
Constraints
- (1) $0 \leq p < n$
- (2) $0 \leq l \leq r \leq n$
Complexity
- $O(\log n)$
max_right
(1) int seg.max_right<g>(int l)
(2 ) int seg.max_right<G>(int l, G g)
- (1): It applies a binary search on the segment tree. The function
bool g(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index r that satisfies both of the followings.
r = lorg(op(a[l], a[l + 1], ..., a[r - 1])) = truer = norg(op(a[l], a[l + 1], ..., a[r])) = false
If g is monotone, this is the maximum r that satisfies g(op(a[l], a[l + 1], ..., a[r - 1])) = true.
Constraints
- if
gis called with the same argument, it returns the same value, i.e.,ghas no side effect. g(e()) = true- $0 \leq l \leq n$
Complexity
- $O(\log n)$
min_left
(1) int seg.min_left<g>(int r)
(2 ) int seg.min_left<G>(int r, G g)
- (1): It applies a binary search on the segment tree. The function
bool g(S x)should be defined. - (2): The function object that takes
Sas the argument and returnsboolshould be defined.
It returns an index l that satisfies both of the following.
l = rorg(op(a[l], a[l + 1], ..., a[r - 1])) = truel = 0org(op(a[l - 1], a[l], ..., a[r - 1])) = false
If g is monotone, this is the minimum l that satisfies g(op(a[l], a[l + 1], ..., a[r - 1])) = true.
Constraints
- if
gis called with the same argument, it returns the same value, i.e.,ghas no side effect. g(e()) = true- $0 \leq r \leq n$
Complexity
- $O(\log n)$
Examples
AC code of https://atcoder.jp/contests/practice2/tasks/practice2_k
AC code of https://atcoder.jp/contests/practice2/tasks/practice2_l