rcpl

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

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: Dual Segment Tree (双対セグメント木)
(segment_tree/dual_segment_tree.hpp)

普通のセグメント木+双対セグメント木=遅延セグメント木というイメージ 普通のセグメント木では区間取得 (prod) が、双対セグメント木では区間作用 (apply) ができる

基本的に algebra/monoid 以下のファイルをインクルードして使う

// 区間更新 1 点取得
#include "algebra/monoid/monoid_assign.hpp"
#include "data_structure/dual_segment_tree.hpp"
int main() {
    constexpr int NONE = std::numeric_limits<int>::max();
    vector<int> A;
    DualSegmentTree<MonoidAssign<int, NONE>> seg(A);
}

Depends on

Verified with

Code

#pragma once

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

#include <cassert>
#include <vector>

// Dual Segment Tree
template <class MF> struct DualSegmentTree {
  public:
    using F = typename MF::value_type;

    DualSegmentTree() = default;

    explicit DualSegmentTree(int n)
        : DualSegmentTree(std::vector<F>(n, MF::identity())) {}

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

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

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

    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);
        lz[p] = f;
    }

    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;
        }
    }

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

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

    void all_apply(int k, const F& f) { 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/dual_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 5 "segment_tree/dual_segment_tree.hpp"

#line 7 "segment_tree/dual_segment_tree.hpp"
#include <vector>

// Dual Segment Tree
template <class MF> struct DualSegmentTree {
  public:
    using F = typename MF::value_type;

    DualSegmentTree() = default;

    explicit DualSegmentTree(int n)
        : DualSegmentTree(std::vector<F>(n, MF::identity())) {}

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

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

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

    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);
        lz[p] = f;
    }

    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;
        }
    }

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

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

    void all_apply(int k, const F& f) { 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