This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "ds/lazy_segtree.hpp"
#pragma once
#include "algebra.hpp"
#include "bit/ctz.hpp"
#include "bit/ilog2.hpp"
template <class M, class A, class F>
class lazy_segment_tree {
public:
using value_type = typename M::type;
using actor_type = typename A::type;
template <class Iter>
lazy_segment_tree(Iter first, Iter last, M m = M(), A a = A(), F f = F())
: m(m),
a(a),
f(f),
data((last - first) * 2),
lazy(last - first, a.unit()) {
copy(first, last, data.begin() + (last - first));
init();
}
lazy_segment_tree(const vector<value_type>& data, M m = M(), A a = A(),
F f = F())
: lazy_segment_tree(all(data), m, a, f) {}
lazy_segment_tree(int n = 0, M m = M(), A a = A(), F f = F())
: m(m), a(a), f(f), data(n * 2, m.unit()), lazy(n, a.unit()) {}
int size() const { return data.size() / 2; }
value_type prod(int l, int r) {
flush(trunc(l + size()));
flush(trunc(r + size()) - 1);
value_type accl = m.unit(), accr = m.unit();
for (l += size(), r += size(); l < r; l >>= 1, r >>= 1) {
if (l & 1) accl = m.op(accl, data[l++]);
if (r & 1) accr = m.op(data[--r], accr);
}
return m.op(accl, accr);
}
void apply(int l, int r, actor_type x) {
#if __cpp_concepts
if constexpr (requires { A::operator ==; }) {
if (x == a.unit()) return;
}
#endif
flush(trunc(l + size()));
flush(trunc(r + size()) - 1);
for (int L = l + size(), R = r + size(); L < R; L >>= 1, R >>= 1) {
if (L & 1) apply(L++, x);
if (R & 1) apply(--R, x);
}
build(trunc(l + size()));
build(trunc(r + size()) - 1);
}
void mul(int i, value_type v) {
exec(i, [&](value_type& e) { e = m.op(e, v); });
}
void set(int i, value_type v) {
exec(i, [&](value_type& e) { e = v; });
}
template <class G>
void exec(int i, G f) {
flush(i + size());
f(data[i + size()]);
build(i + size());
}
private:
M m;
A a;
F f;
vector<value_type> data;
vector<actor_type> lazy;
void init() {
for (int i = size(); --i;) data[i] = m.op(data[i << 1], data[i << 1 | 1]);
}
void apply(int i, actor_type x) {
data[i] = f(data[i], x);
if (i < size()) lazy[i] = a.op(lazy[i], x);
}
void push(int i) {
#if __cpp_concepts
if constexpr (requires { A::operator ==; }) {
if (lazy[i] == a.unit()) return;
}
#endif
apply(i << 1, lazy[i]);
apply(i << 1 | 1, lazy[i]);
lazy[i] = a.unit();
}
void upd(int i) { data[i] = m.op(data[i << 1], data[i << 1 | 1]); }
void flush(int i) {
if (i)
for (int s = ilog2(i); s > 0; s--) push(i >> s);
}
void build(int i) {
for (; i >>= 1;) upd(i);
}
static int trunc(int a) { return a >> ctz(a); }
};
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "algebra.hpp"
#define CONST(val) [=] { return val; }
#define WRAP_FN(func) \
[](auto&&... args) { return func(forward<decltype(args)>(args)...); }
template <class Unit, class Op>
struct monoid : private Unit, private Op {
using type = decltype(declval<Unit>()());
monoid(Unit unit, Op op) : Unit(unit), Op(op) {}
type unit() const { return Unit::operator()(); }
type op(type a, type b) const { return Op::operator()(a, b); }
};
template <class Unit, class Op, class Inv>
struct group : monoid<Unit, Op>, private Inv {
using type = typename monoid<Unit, Op>::type;
group(Unit unit, Op op, Inv inv) : monoid<Unit, Op>(unit, op), Inv(inv) {}
type inv(type a) const { return Inv::operator()(a); }
};
template <class T>
struct addition {
using type = T;
type unit() const { return 0; }
type op(type a, type b) const { return a + b; }
type inv(type a) const { return -a; }
};
template <class T>
struct maximum {
using type = T;
type unit() const { return numeric_limits<T>::min(); }
type op(type a, type b) const { return a > b ? a : b; }
};
template <class T>
struct minimum {
using type = T;
type unit() const { return numeric_limits<T>::max(); }
type op(type a, type b) const { return a > b ? b : a; }
};
template <class T, T nul = -1>
struct assign {
using type = T;
type unit() const { return nul; }
type op(type a, type b) const { return b == nul ? a : b; }
};
#line 3 "bit/ctz.hpp"
#pragma GCC target("bmi")
template <class T>
int ctz(T x) {
if (!x) return sizeof(T) * 8;
if constexpr (sizeof(T) <= sizeof(unsigned)) {
return __builtin_ctz((unsigned)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
return __builtin_ctzll((unsigned long long)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
unsigned long long y = x;
return y ? ctz(y)
: sizeof(y) * 8 + ctz((unsigned long long)(x >> sizeof(y) * 8));
}
}
#line 3 "bit/clz.hpp"
#pragma GCC target("lzcnt")
template <class T>
int clz(T x) {
if (!x) return sizeof(T) * 8;
if constexpr (sizeof(T) <= sizeof(unsigned)) {
return __builtin_clz((unsigned)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
return __builtin_clzll((unsigned long long)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
}
}
#line 4 "bit/ilog2.hpp"
template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }
template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }
template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }
template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 5 "ds/lazy_segtree.hpp"
template <class M, class A, class F>
class lazy_segment_tree {
public:
using value_type = typename M::type;
using actor_type = typename A::type;
template <class Iter>
lazy_segment_tree(Iter first, Iter last, M m = M(), A a = A(), F f = F())
: m(m),
a(a),
f(f),
data((last - first) * 2),
lazy(last - first, a.unit()) {
copy(first, last, data.begin() + (last - first));
init();
}
lazy_segment_tree(const vector<value_type>& data, M m = M(), A a = A(),
F f = F())
: lazy_segment_tree(all(data), m, a, f) {}
lazy_segment_tree(int n = 0, M m = M(), A a = A(), F f = F())
: m(m), a(a), f(f), data(n * 2, m.unit()), lazy(n, a.unit()) {}
int size() const { return data.size() / 2; }
value_type prod(int l, int r) {
flush(trunc(l + size()));
flush(trunc(r + size()) - 1);
value_type accl = m.unit(), accr = m.unit();
for (l += size(), r += size(); l < r; l >>= 1, r >>= 1) {
if (l & 1) accl = m.op(accl, data[l++]);
if (r & 1) accr = m.op(data[--r], accr);
}
return m.op(accl, accr);
}
void apply(int l, int r, actor_type x) {
#if __cpp_concepts
if constexpr (requires { A::operator ==; }) {
if (x == a.unit()) return;
}
#endif
flush(trunc(l + size()));
flush(trunc(r + size()) - 1);
for (int L = l + size(), R = r + size(); L < R; L >>= 1, R >>= 1) {
if (L & 1) apply(L++, x);
if (R & 1) apply(--R, x);
}
build(trunc(l + size()));
build(trunc(r + size()) - 1);
}
void mul(int i, value_type v) {
exec(i, [&](value_type& e) { e = m.op(e, v); });
}
void set(int i, value_type v) {
exec(i, [&](value_type& e) { e = v; });
}
template <class G>
void exec(int i, G f) {
flush(i + size());
f(data[i + size()]);
build(i + size());
}
private:
M m;
A a;
F f;
vector<value_type> data;
vector<actor_type> lazy;
void init() {
for (int i = size(); --i;) data[i] = m.op(data[i << 1], data[i << 1 | 1]);
}
void apply(int i, actor_type x) {
data[i] = f(data[i], x);
if (i < size()) lazy[i] = a.op(lazy[i], x);
}
void push(int i) {
#if __cpp_concepts
if constexpr (requires { A::operator ==; }) {
if (lazy[i] == a.unit()) return;
}
#endif
apply(i << 1, lazy[i]);
apply(i << 1 | 1, lazy[i]);
lazy[i] = a.unit();
}
void upd(int i) { data[i] = m.op(data[i << 1], data[i << 1 | 1]); }
void flush(int i) {
if (i)
for (int s = ilog2(i); s > 0; s--) push(i >> s);
}
void build(int i) {
for (; i >>= 1;) upd(i);
}
static int trunc(int a) { return a >> ctz(a); }
};