This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "structure/segment-tree/segment-tree-beats.hpp"
(1) SegmentTreeBeats< BeatsMonoid >(BeatsMonoid m, int n)
(2) SegmentTreeBeats< BeatsMonoid >(BeatsMonoid m, const vector< S > &v)
m
、サイズ n
で初期化します。各要素には単位元 m.e()
が格納されます。m
、配列 v
で初期化します。BeatsMonoid
は、次の構造体と関数を持つ構造体です。
struct BeatsMonoid {
using S = ?;
using F = ?;
static constexpr S op(const S& a, const S& b) {}
static constexpr bool fail(const S& a) {}
static constexpr S e() {}
static constexpr S mapping(const S &x, const F &f) {}
static constexpr F composition(const F &f, const F &g) {}
static constexpr F id() {}
};
S
F
S op(S a, S b)
bool fail(S a)
e()
S mapping(S x, F f)
F composition(F f, F g)
id()
作用素 F
は、単位元 id()
と比較するために !=
演算子が定義されている必要があります。
LambdaBeatsMonoid
は、ラムダ式を受け取って、構造体 BeatsMonoid
のようにふるまう構造体です 。LambdaBeatsMonoid
の引数に S op(S a, S b)
、bool fail(S a)
、e()
、S mapping(S x, F f)
、F composition(F f, F g)
、id()
の順で渡すことで初期化できます。
template< typename Op, typename Fail, typename E, typename Mapping, typename Composition, typename Id >
LambdaBeatsMonoid(Op _op, Fail _fail, E _e, Mapping _mapping, Composition _composition, Id _id)
-> LambdaBeatsMonoid< decltype(_e()), Op, Fail, E, decltype(_id()), Mapping, Composition, Id >;
void build(const vector<S> &v)
配列 v
で初期化します。
n
と v
の長さが一致するvoid set(int k, const S &x)
k
番目の要素を x
に変更します。
S get(int k)
k
番目の要素を返します。
S operator[](int k)
k
番目の要素を返します。
S prod(int l, int r)
区間 $[l, r)$ に対して二項演算した結果を返します。
S all_prod() const
すべての要素を二項演算した結果を返します。
(1) void apply(int k, const F &f)
(2) void apply(int l, int r, const F &f)
k
番目の要素に対して作用素を適用します。#include "../class/beats-monoid.hpp"
template <typename BeatsMonoid>
struct SegmentTreeBeats {
using S = typename BeatsMonoid::S;
using F = typename BeatsMonoid::F;
private:
BeatsMonoid m;
int n{}, sz{}, height{};
vector<S> data;
vector<F> lazy;
inline void update(int k) {
data[k] = m.op(data[2 * k + 0], data[2 * k + 1]);
}
inline void apply_at(int k, const F &x) {
data[k] = m.mapping(data[k], x);
if (k < sz) {
lazy[k] = m.composition(lazy[k], x);
if (m.fail(data[k])) {
propagate(k);
update(k);
}
}
}
inline void propagate(int k) {
if (lazy[k] != m.id()) {
apply_at(2 * k + 0, lazy[k]);
apply_at(2 * k + 1, lazy[k]);
lazy[k] = m.id();
}
}
public:
SegmentTreeBeats() = default;
explicit SegmentTreeBeats(BeatsMonoid m, int n) : m(m), n(n) {
sz = 1;
height = 0;
while (sz < n) sz <<= 1, height++;
data.assign(2 * sz, m.e());
lazy.assign(2 * sz, m.id());
}
explicit SegmentTreeBeats(BeatsMonoid m, const vector<S> &v)
: SegmentTreeBeats(m, v.size()) {
build(v);
}
void build(const vector<S> &v) {
assert(n == (int)v.size());
for (int k = 0; k < n; k++) data[k + sz] = v[k];
for (int k = sz - 1; k > 0; k--) update(k);
}
void set(int k, const S &x) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
S get(int k) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
return data[k];
}
S operator[](int k) { return get(k); }
S prod(int l, int r) {
if (l >= r) return m.e();
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
S L = m.e(), R = m.e();
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = m.op(L, data[l++]);
if (r & 1) R = m.op(data[--r], R);
}
return m.op(L, R);
}
S all_prod() const { return data[1]; }
void apply(int k, const F &f) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = m.mapping(data[k], f);
for (int i = 1; i <= height; i++) update(k >> i);
}
void apply(int l, int r, const F &f) {
if (l >= r) return;
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
{
int l2 = l, r2 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply_at(l++, f);
if (r & 1) apply_at(--r, f);
}
l = l2, r = r2;
}
for (int i = 1; i <= height; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
};
#line 2 "structure/class/beats-monoid.hpp"
template <typename S2, typename Op, typename Fail, typename E, typename F2,
typename Mapping, typename Composition, typename Id>
struct LambdaBeatsMonoid {
using S = S2;
using F = F2;
S op(const S &a, const S &b) const { return _op(a, b); }
bool fail(const S &a) const { return _fail(a); }
S e() const { return _e(); }
S mapping(const S &x, const F &f) const { return _mapping(x, f); }
F composition(const F &f, const F &g) const { return _composition(f, g); }
F id() const { return _id(); }
LambdaBeatsMonoid(Op _op, Fail _fail, E _e, Mapping _mapping,
Composition _composition, Id _id)
: _op(_op),
_fail(_fail),
_e(_e),
_mapping(_mapping),
_composition(_composition),
_id(_id) {}
private:
Op _op;
Fail _fail;
E _e;
Mapping _mapping;
Composition _composition;
Id _id;
};
template <typename Op, typename Fail, typename E, typename Mapping,
typename Composition, typename Id>
LambdaBeatsMonoid(Op _op, Fail _fail, E _e, Mapping _mapping,
Composition _composition, Id _id)
-> LambdaBeatsMonoid<decltype(_e()), Op, Fail, E, decltype(_id()), Mapping,
Composition, Id>;
/*
struct BeatsMonoid {
using S = ?;
using F = ?;
static constexpr S op(const S& a, const S& b) {}
static constexpr bool fail(const S& a) {}
static constexpr S e() {}
static constexpr S mapping(const S &x, const F &f) {}
static constexpr F composition(const F &f, const F &g) {}
static constexpr F id() {}
};
*/
#line 2 "structure/segment-tree/segment-tree-beats.hpp"
template <typename BeatsMonoid>
struct SegmentTreeBeats {
using S = typename BeatsMonoid::S;
using F = typename BeatsMonoid::F;
private:
BeatsMonoid m;
int n{}, sz{}, height{};
vector<S> data;
vector<F> lazy;
inline void update(int k) {
data[k] = m.op(data[2 * k + 0], data[2 * k + 1]);
}
inline void apply_at(int k, const F &x) {
data[k] = m.mapping(data[k], x);
if (k < sz) {
lazy[k] = m.composition(lazy[k], x);
if (m.fail(data[k])) {
propagate(k);
update(k);
}
}
}
inline void propagate(int k) {
if (lazy[k] != m.id()) {
apply_at(2 * k + 0, lazy[k]);
apply_at(2 * k + 1, lazy[k]);
lazy[k] = m.id();
}
}
public:
SegmentTreeBeats() = default;
explicit SegmentTreeBeats(BeatsMonoid m, int n) : m(m), n(n) {
sz = 1;
height = 0;
while (sz < n) sz <<= 1, height++;
data.assign(2 * sz, m.e());
lazy.assign(2 * sz, m.id());
}
explicit SegmentTreeBeats(BeatsMonoid m, const vector<S> &v)
: SegmentTreeBeats(m, v.size()) {
build(v);
}
void build(const vector<S> &v) {
assert(n == (int)v.size());
for (int k = 0; k < n; k++) data[k + sz] = v[k];
for (int k = sz - 1; k > 0; k--) update(k);
}
void set(int k, const S &x) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
S get(int k) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
return data[k];
}
S operator[](int k) { return get(k); }
S prod(int l, int r) {
if (l >= r) return m.e();
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
S L = m.e(), R = m.e();
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = m.op(L, data[l++]);
if (r & 1) R = m.op(data[--r], R);
}
return m.op(L, R);
}
S all_prod() const { return data[1]; }
void apply(int k, const F &f) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = m.mapping(data[k], f);
for (int i = 1; i <= height; i++) update(k >> i);
}
void apply(int l, int r, const F &f) {
if (l >= r) return;
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
{
int l2 = l, r2 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply_at(l++, f);
if (r & 1) apply_at(--r, f);
}
l = l2, r = r2;
}
for (int i = 1; i <= height; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
};