rcpl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: Lazy Segment Tree (遅延セグメント木)
(segment_tree/lazy_segment_tree.hpp)

長さ $n$ の配列 $a$ に対し

を $O(\log n)$ 時間で処理することが出来ます。

テンプレート引数として、モノイド $(S, \cdot)$ と写像の集合 $F$ を AM として受け取ります。 モノイドとは以下の条件を満たす代数構造です。

写像は以下の条件を満たします。

例えば、$\cdot$ として $\max$ を、$f(x)$ として加算を計算するモノイドは ここ に定義されています。

計算量は $\cdot$, $e$, $f(x)$, $\circ$, $\mathrm{id}$ が定数時間で計算できると仮定したときのものを記述します。

コンストラクタ

(1) LazySegmentTree<M> seg(int n)
(2) LazySegmentTree<M> seg(std::vector<S> v)

(1)

長さ $n$ の数列 $a$ を作ります。初期値は全部 $e$ です。

(2)

長さ $ n = \left| v \right| $ の数列 $a$ を作ります。 $v$ の内容が初期値となります。

計算量

set

void seg.set(int p, S x)

$a_p$ に $x$ を代入します。

制約

計算量

add

void seg.add(int p, S x)

$a_p$ に $a_p \cdot x$ を代入します。

制約

計算量

get

(1) S seg.get(int p)
(2) S seg[int p]

$a_p$ を返します。

制約

計算量

prod

S seg.prod(int l, int r)

$a_l \cdot … \cdot a_{r - 1}$ を、モノイドの性質を満たしていると仮定して計算します。 $l = r$ のときは $e$ を返します。

制約

計算量

all_prod

S seg.all_prod()

$a_0 \cdot …\cdot a_{n - 1}$ を計算します。 $n = 0$ のときは $e$ を返します。

計算量

apply

(1) void seg.apply(int p, F f)
(2) void seg.apply(int l, int r, F f)

(1)

$a_p$ に $f(a_p)$ を代入します。

(2)

$ i = l, …, r-1 $ について $a_i$ に $f(a_i)$ を代入します。

制約

(1)

(2)

計算量

max_right

int seg.max_right<G>(int l, G g)

S を引数にとり bool を返す関数オブジェクトを渡して使用します。

以下の条件を両方満たす $r$ を(いずれか一つ)返します。

$g$ が単調だとすれば、$g(a_{l} \cdot a_{l + 1} \cdot … \cdot a_{r - 1}) = true$ となる最大の $r$ と解釈することが可能です。

制約

計算量

min_left

int seg.min_left<G>(int r, G g)

S を引数にとり bool を返す関数オブジェクトを渡して使用します。

以下の条件を両方満たす $l$ を(いずれか一つ)返します。

$g$ が単調だとすれば、$g(a_{l} \cdot a_{l + 1} \cdot … \cdot a_{r - 1}) = true$ となる最小の $l$ と解釈することが可能です。

制約

計算量

make_vector

std::vector<S> seg.make_vector()

現在の数列 $a$ を返します。

計算量

Depends on

Verified with

Code

#pragma once

#include "../misc/bit_ceil.hpp"
#include "../misc/countr_zero.hpp"
#include "../misc/topbit.hpp"

#include <cassert>
#include <vector>

// Lazy Segment Tree
template <class AM> struct LazySegmentTree {
  public:
    using MS = typename AM::MS;
    using MF = typename AM::MF;
    using S = typename MS::value_type;
    using F = typename MF::value_type;

    LazySegmentTree() = default;

    explicit LazySegmentTree(int n)
        : LazySegmentTree(std::vector<S>(n, MS::identity())) {}

    explicit LazySegmentTree(const std::vector<S>& v) : n((int)(v.size())) {
        size = bit_ceil(n);
        log = countr_zero(size);
        d = std::vector<S>(size << 1, MS::identity());
        lz = std::vector<F>(size, MF::identity());
        for (int i = 0; i < n; i++) d[i + size] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void add(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = MS::operation(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S operator[](int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S get(int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        if (l == r) return MS::identity();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = MS::identity(), smr = MS::identity();
        while (l < r) {
            if (l & 1) sml = MS::operation(sml, d[l++]);
            if (r & 1) smr = MS::operation(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return MS::operation(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, const F& f) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = AM::mapping(f, d[p], 1);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void apply(int l, int r, const F& f) {
        assert(0 <= l and l <= r and r <= n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <class G> int max_right(int l, G& g) {
        assert(0 <= l and l <= n);
        assert(g(MS::identity()));
        if (l == n) return n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = MS::identity();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!g(MS::operation(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l <<= 1;
                    if (g(MS::operation(sm, d[l]))) {
                        sm = MS::operation(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = MS::operation(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class G> int min_left(int r, G& g) {
        assert(0 <= r and r <= n);
        assert(g(MS::identity()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = MS::identity();
        do {
            r--;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!g(MS::operation(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (r << 1) | 1;
                    if (g(MS::operation(d[r], sm))) {
                        sm = MS::operation(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = MS::operation(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    std::vector<S> make_vector() {
        std::vector<S> vec(n);
        for (int i = 0; i < n; i++) vec[i] = get(i);
        return vec;
    }

  private:
    int n, log, size;
    std::vector<S> d;
    std::vector<F> lz;

    inline void update(int k) {
        d[k] = MS::operation(d[k << 1], d[(k << 1) | 1]);
    }

    void all_apply(int k, const F& f) {
        d[k] = AM::mapping(f, d[k], 1 << (log - topbit(k)));
        // MF::operation(f, g), g(f(x))
        if (k < size) lz[k] = MF::operation(lz[k], f);
    }

    void push(int k) {
        all_apply(k << 1, lz[k]);
        all_apply((k << 1) | 1, lz[k]);
        lz[k] = MF::identity();
    }
};
#line 2 "segment_tree/lazy_segment_tree.hpp"

#line 2 "misc/bit_ceil.hpp"

#include <cassert>

#if __cplusplus >= 202002L
#include <bit>
#endif

// bit_ceil
// (0, 1, 2, 3, 4) -> (1, 1, 2, 4, 4)
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
unsigned int bit_ceil(unsigned int x) {
    unsigned int p = 1;
    while (p < x) p *= 2;
    return p;
}
unsigned long long int bit_ceil(unsigned long long int x) {
    unsigned long long int p = 1;
    while (p < x) p *= 2;
    return p;
}
#endif
int bit_ceil(int x) {
    assert(x >= 0);
    return bit_ceil((unsigned int)(x));
}
long long int bit_ceil(long long int x) {
    assert(x >= 0);
    return bit_ceil((unsigned long long int)(x));
}
#line 2 "misc/countr_zero.hpp"

#if __cplusplus >= 202002L
#include <bit>
#endif

// countr_zero
// (000, 001, 010, 011, 100) -> (32, 0, 1, 0, 2)
#if __cplusplus >= 202002L
using std::countr_zero;
#else
int countr_zero(unsigned int x) {
    return x == 0 ? 32 : __builtin_ctz(x);
}
int countr_zero(unsigned long long int x) {
    return x == 0 ? 64 : __builtin_ctzll(x);
}
#endif
int countr_zero(int x) { return countr_zero((unsigned int)(x)); }
int countr_zero(long long int x) {
    return countr_zero((unsigned long long int)(x));
}
#line 2 "misc/topbit.hpp"

#line 2 "misc/countl_zero.hpp"

#if __cplusplus >= 202002L
#include <bit>
#endif

// countl_zero
// (000, 001, 010, 011, 100) -> (32, 31, 30, 30, 29)
#if __cplusplus >= 202002L
using std::countl_zero;
#else
int countl_zero(unsigned int x) {
    return x == 0 ? 32 : __builtin_clz(x);
}
int countl_zero(unsigned long long int x) {
    return x == 0 ? 64 : __builtin_clzll(x);
}
#endif
int countl_zero(int x) { return countl_zero((unsigned int)(x)); }
int countl_zero(long long int x) {
    return countl_zero((unsigned long long int)(x));
}
#line 4 "misc/topbit.hpp"

// topbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return 31 - countl_zero(x); }
int topbit(unsigned int x) { return 31 - countl_zero(x); }
int topbit(long long int x) { return 63 - countl_zero(x); }
int topbit(unsigned long long int x) { return 63 - countl_zero(x); }
#line 6 "segment_tree/lazy_segment_tree.hpp"

#line 8 "segment_tree/lazy_segment_tree.hpp"
#include <vector>

// Lazy Segment Tree
template <class AM> struct LazySegmentTree {
  public:
    using MS = typename AM::MS;
    using MF = typename AM::MF;
    using S = typename MS::value_type;
    using F = typename MF::value_type;

    LazySegmentTree() = default;

    explicit LazySegmentTree(int n)
        : LazySegmentTree(std::vector<S>(n, MS::identity())) {}

    explicit LazySegmentTree(const std::vector<S>& v) : n((int)(v.size())) {
        size = bit_ceil(n);
        log = countr_zero(size);
        d = std::vector<S>(size << 1, MS::identity());
        lz = std::vector<F>(size, MF::identity());
        for (int i = 0; i < n; i++) d[i + size] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void add(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = MS::operation(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S operator[](int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S get(int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        if (l == r) return MS::identity();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = MS::identity(), smr = MS::identity();
        while (l < r) {
            if (l & 1) sml = MS::operation(sml, d[l++]);
            if (r & 1) smr = MS::operation(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return MS::operation(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, const F& f) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = AM::mapping(f, d[p], 1);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void apply(int l, int r, const F& f) {
        assert(0 <= l and l <= r and r <= n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <class G> int max_right(int l, G& g) {
        assert(0 <= l and l <= n);
        assert(g(MS::identity()));
        if (l == n) return n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = MS::identity();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!g(MS::operation(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l <<= 1;
                    if (g(MS::operation(sm, d[l]))) {
                        sm = MS::operation(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = MS::operation(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class G> int min_left(int r, G& g) {
        assert(0 <= r and r <= n);
        assert(g(MS::identity()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = MS::identity();
        do {
            r--;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!g(MS::operation(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (r << 1) | 1;
                    if (g(MS::operation(d[r], sm))) {
                        sm = MS::operation(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = MS::operation(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    std::vector<S> make_vector() {
        std::vector<S> vec(n);
        for (int i = 0; i < n; i++) vec[i] = get(i);
        return vec;
    }

  private:
    int n, log, size;
    std::vector<S> d;
    std::vector<F> lz;

    inline void update(int k) {
        d[k] = MS::operation(d[k << 1], d[(k << 1) | 1]);
    }

    void all_apply(int k, const F& f) {
        d[k] = AM::mapping(f, d[k], 1 << (log - topbit(k)));
        // MF::operation(f, g), g(f(x))
        if (k < size) lz[k] = MF::operation(lz[k], f);
    }

    void push(int k) {
        all_apply(k << 1, lz[k]);
        all_apply((k << 1) | 1, lz[k]);
        lz[k] = MF::identity();
    }
};
Back to top page