C++template <typename S, S (*op)(S, S), S (*e)(), typename F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>
struct LazySegTree {
private:
int32_t n, sz, lg;
alignas(32) std::vector<S> d;
alignas(32) std::vector<F> lz;
__attribute__((always_inline))
void pushup(int32_t k) noexcept {
d[k] = op(d[k << 1], d[k << 1 | 1]);
}
__attribute__((always_inline))
void all_apply(int32_t k, F f) noexcept {
d[k] = mapping(f, d[k]);
if (k < sz) lz[k] = composition(f, lz[k]);
}
__attribute__((always_inline))
void pushdown(int32_t k) noexcept {
all_apply(k << 1, lz[k]);
all_apply(k << 1 | 1, lz[k]);
lz[k] = id();
}
public:
LazySegTree() noexcept : n(0), sz(0), lg(0) {}
explicit LazySegTree(const std::vector<S>& v) noexcept {
n = static_cast<int32_t>(v.size());
lg = 0;
while ((1U << lg) < static_cast<uint32_t>(n)) ++lg;
sz = 1 << lg;
d.resize(sz << 1, e());
lz.resize(sz, id());
for (int32_t i = 0; i < n; ++i) d[sz + i] = v[i];
for (int32_t i = sz - 1; i; --i) pushup(i);
}
__attribute__((hot))
void set(int32_t p, S x) noexcept {
p += sz;
for (int32_t i = lg; i; --i) pushdown(p >> i);
d[p] = x;
for (int32_t i = 1; i ^ (lg + 1); ++i) pushup(p >> i);
}
__attribute__((hot))
S get(int32_t p) noexcept {
p += sz;
for (int32_t i = lg; i; --i) pushdown(p >> i);
return d[p];
}
__attribute__((hot))
S prod(int32_t l, int32_t r) noexcept {
if (l == r) return e();
l += sz;
r += sz;
for (int32_t i = lg; i; --i) {
if (((l >> i) << i) != l) pushdown(l >> i);
if (((r >> i) << i) != r) pushdown(r >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
__attribute__((always_inline))
S all_prod() const noexcept {
return d[1];
}
__attribute__((hot))
void apply(int32_t p, F f) noexcept {
p += sz;
for (int32_t i = lg; i; --i) pushdown(p >> i);
d[p] = mapping(f, d[p]);
for (int32_t i = 1; i ^ (lg + 1); ++i) pushup(p >> i);
}
__attribute__((hot))
void apply(int32_t l, int32_t r, F f) noexcept {
if (l == r) return;
l += sz;
r += sz;
int32_t l0 = l, r0 = r;
for (int32_t i = lg; i; --i) {
if (((l0 >> i) << i) != l0) pushdown(l0 >> i);
if (((r0 >> i) << i) != r0) pushdown(r0 >> i);
}
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l0;
r = r0;
for (int32_t i = 1; i ^ (lg + 1); ++i) {
if (((l >> i) << i) != l) pushup(l >> i);
if (((r >> i) << i) != r) pushup(r >> i);
}
}
};