This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "ds/range_fenwick.hpp"
#pragma once
#include "ds/fenwick.hpp"
template <class M, class F = multiplies<>>
class range_fenwick_tree {
public:
using value_type = typename M::type;
range_fenwick_tree(vector<value_type> data, M m = M(), F mul = F())
: m(m), mul(mul), tri(data.size(), m), rect(move(data), m) {}
range_fenwick_tree(int n = 0, M m = M(), F mul = F())
: m(m), mul(mul), rect(n, m), tri(n, m) {}
int size() const { return rect.size(); }
void clear() { rect.clear(), tri.clear(); }
void add(int i, value_type v) { rect.add(i, v); }
void add(int l, int r, value_type v) {
tri.add(l, v), tri.sub(r, v);
rect.sub(l, mul(v, l)), rect.add(r, mul(v, r));
}
void sub(int i, value_type v) { add(i, m.inv(v)); }
void sub(int l, int r, value_type v) { add(l, r, m.inv(v)); }
value_type sum(int r) const { return m.op(rect.sum(r), mul(tri.sum(r), r)); }
value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
// template <class F>
// int partition_point(F pred = F()) const {
// int i = 0;
// value_type s = m.unit();
// if (!pred(s)) return i;
// for (int w = bit_floor(rect.data.size()); w; w >>= 1) {
// if (i + w < rect.data.size()) {
// value_type s2 =
// m.op(m.op(s, rect.data[i + w]), mul(tri.data[i + w], i + w + 1));
// if (pred(s2)) i += w, s = s2;
// }
// }
// return i + 1;
// }
// // min i s.t. !comp(sum(i), x)
// template <class Comp = less<value_type>>
// int lower_bound(value_type x, Comp comp = Comp()) const {
// return partition_point([&](value_type s) { return comp(s, x); });
// }
private:
M m;
F mul;
fenwick_tree<M> rect, tri;
};
#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/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 4 "ds/fenwick.hpp"
template <class M>
class fenwick_tree {
public:
using value_type = typename M::type;
fenwick_tree() = default;
fenwick_tree(vector<value_type> v, M m = M()) : m(m), data(move(v)) {
data.insert(data.cbegin(), m.unit());
for (int i = 1; i < data.size(); i++) {
if (i + lsb(i) < data.size())
data[i + lsb(i)] = m.op(data[i + lsb(i)], data[i]);
}
}
template <class Iter>
fenwick_tree(Iter f, Iter l, M m = M())
: fenwick_tree(vector<value_type>(f, l), m) {}
fenwick_tree(int n, M m = M()) : m(m), data(n + 1, m.unit()) {}
int size() const { return data.size() - 1; }
void clear() { fill(data.begin(), data.end(), m.unit()); }
void add(int i, value_type v) {
for (i++; i < data.size(); i += lsb(i)) data[i] = m.op(data[i], v);
}
void sub(int i, value_type v) { add(i, m.inv(v)); }
void assign(int i, value_type v) { add(i, m.op(v, m.inv(sum(i, i + 1)))); }
value_type sum(int r) const {
value_type res = m.unit();
for (; r; r -= lsb(r)) res = m.op(res, data[r]);
return res;
}
value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
template <class F>
int partition_point(F pred = F()) const {
int i = 0;
value_type s = m.unit();
if (!pred(s)) return i;
for (int w = bit_floor(data.size()); w; w >>= 1) {
if (i + w < data.size()) {
value_type s2 = m.op(s, data[i + w]);
if (pred(s2)) i += w, s = s2;
}
}
return i + 1;
}
// min i s.t. sum(i) >= x
template <class Comp = less<value_type>>
int lower_bound(value_type x, Comp comp = Comp()) const {
return partition_point([&](value_type s) { return comp(s, x); });
}
private:
M m;
vector<value_type> data;
static int lsb(int a) { return a & -a; }
};
#line 3 "ds/range_fenwick.hpp"
template <class M, class F = multiplies<>>
class range_fenwick_tree {
public:
using value_type = typename M::type;
range_fenwick_tree(vector<value_type> data, M m = M(), F mul = F())
: m(m), mul(mul), tri(data.size(), m), rect(move(data), m) {}
range_fenwick_tree(int n = 0, M m = M(), F mul = F())
: m(m), mul(mul), rect(n, m), tri(n, m) {}
int size() const { return rect.size(); }
void clear() { rect.clear(), tri.clear(); }
void add(int i, value_type v) { rect.add(i, v); }
void add(int l, int r, value_type v) {
tri.add(l, v), tri.sub(r, v);
rect.sub(l, mul(v, l)), rect.add(r, mul(v, r));
}
void sub(int i, value_type v) { add(i, m.inv(v)); }
void sub(int l, int r, value_type v) { add(l, r, m.inv(v)); }
value_type sum(int r) const { return m.op(rect.sum(r), mul(tri.sum(r), r)); }
value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
// template <class F>
// int partition_point(F pred = F()) const {
// int i = 0;
// value_type s = m.unit();
// if (!pred(s)) return i;
// for (int w = bit_floor(rect.data.size()); w; w >>= 1) {
// if (i + w < rect.data.size()) {
// value_type s2 =
// m.op(m.op(s, rect.data[i + w]), mul(tri.data[i + w], i + w + 1));
// if (pred(s2)) i += w, s = s2;
// }
// }
// return i + 1;
// }
// // min i s.t. !comp(sum(i), x)
// template <class Comp = less<value_type>>
// int lower_bound(value_type x, Comp comp = Comp()) const {
// return partition_point([&](value_type s) { return comp(s, x); });
// }
private:
M m;
F mul;
fenwick_tree<M> rect, tri;
};