This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "ds/sparse_table.hpp"
#pragma once
#include "algebra.hpp"
template <class M>
class sparse_table {
public:
using value_type = typename M::type;
sparse_table() = default;
sparse_table(vector<value_type> v, M m = M()) : m(m), data({move(v)}) {
int n = data[0].size();
lg.resize(n + 1);
rep2(i, 2, n + 1) lg[i] = lg[i / 2] + 1;
data.resize(lg[n] + 1);
rep(t, lg[n]) {
int w = 1 << t;
data[t + 1].resize(n - w * 2 + 1);
rep(i, n - w * 2 + 1) data[t + 1][i] = m.op(data[t][i], data[t][i + w]);
}
}
template <class It>
sparse_table(It first, It last, M m = M()) : sparse_table({first, last}, m) {}
value_type prod(int l, int r) {
if (l == r) return m.unit();
int t = lg[r - l];
return m.op(data[t][l], data[t][r - (1 << t)]);
}
value_type disjoint_prod(int l, int r) {
value_type res = m.unit();
while (l != r) {
int t = lg[r - l];
res = m.op(res, data[t][l]);
l += 1 << t;
}
return res;
}
private:
M m;
vector<vector<value_type>> data;
vector<int> lg;
};
#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 "ds/sparse_table.hpp"
template <class M>
class sparse_table {
public:
using value_type = typename M::type;
sparse_table() = default;
sparse_table(vector<value_type> v, M m = M()) : m(m), data({move(v)}) {
int n = data[0].size();
lg.resize(n + 1);
rep2(i, 2, n + 1) lg[i] = lg[i / 2] + 1;
data.resize(lg[n] + 1);
rep(t, lg[n]) {
int w = 1 << t;
data[t + 1].resize(n - w * 2 + 1);
rep(i, n - w * 2 + 1) data[t + 1][i] = m.op(data[t][i], data[t][i + w]);
}
}
template <class It>
sparse_table(It first, It last, M m = M()) : sparse_table({first, last}, m) {}
value_type prod(int l, int r) {
if (l == r) return m.unit();
int t = lg[r - l];
return m.op(data[t][l], data[t][r - (1 << t)]);
}
value_type disjoint_prod(int l, int r) {
value_type res = m.unit();
while (l != r) {
int t = lg[r - l];
res = m.op(res, data[t][l]);
l += 1 << t;
}
return res;
}
private:
M m;
vector<vector<value_type>> data;
vector<int> lg;
};