proconlib

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

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: container/dual_segment_tree.cpp

Depends on

Verified with

Code

#pragma once
#include <cassert>
#include <vector>
#include "../utility/ceil_log2.cpp"
#include "../utility/countr_zero.cpp"
#include "../utility/int_alias.cpp"
#include "../utility/rep.cpp"
#include "../utility/revrep.cpp"

template <class M> class DualSegmentTree {
    using T = typename M::Type;
    int internal_size, logn;
    std::vector<T> lazy;

    void apply(const int k, const T& e) { lazy[k] = M::operation(lazy[k], e); }
    void flush(const int k) {
        apply(2 * k, lazy[k]);
        apply(2 * k + 1, lazy[k]);
        lazy[k] = M::identity();
    }
    void push(const int k) {
        for (const int d : revrep(countr_zero(k) + 1, logn + 1)) flush(k >> d);
    }

  public:
    explicit DualSegmentTree(const int size = 0, const T& value = M::identity())
        : DualSegmentTree(std::vector<T>(size, value)) {}
    explicit DualSegmentTree(const std::vector<T>& vec) : internal_size(vec.size()) {
        logn = ceil_log2(internal_size);
        lazy = std::vector<T>(2 * internal_size, M::identity());
        for (const int i : rep(internal_size)) lazy[i] = vec[i];
    }

    int size() const { return internal_size; }

    void operate(int l, int r, const T& e) {
        assert(0 <= l and l <= r and r <= internal_size);
        l += internal_size;
        r += internal_size;
        push(l);
        push(r);
        while (l < r) {
            if (l & 1) apply(l++, e);
            if (r & 1) apply(--r, e);
            l >>= 1;
            r >>= 1;
        }
    }
    void assign(int i, const T& e) {
        assert(0 <= i and i < internal_size);
        i += internal_size;
        for (const int d : revrep(1, logn + 1)) flush(i >> d);
        lazy[i] = e;
    }

    T get(int i) const {
        assert(0 <= i and i < internal_size);
        i += internal_size;
        T ret = M::identity();
        while (i > 0) {
            ret = M::operation(ret, lazy[i]);
            i >>= 1;
        }
        return ret;
    }
};
#line 2 "container/dual_segment_tree.cpp"
#include <cassert>
#include <vector>
#line 2 "internal/enable_avx2.cpp"

#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"

TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
    return x == 0 ? 64 : __builtin_clzll(x);
#else
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"

TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"

TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
    return x == 0 ? 0 : bit_width(x - 1);
#else
    int e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
#endif
}
#line 2 "utility/countr_zero.cpp"
#include <array>
#line 5 "utility/countr_zero.cpp"

constexpr int countr_zero(u64 x) {
    if (x == 0) return 64;
#ifdef __GNUC__
    return __builtin_ctzll(x);
#else
    constexpr std::array<int, 64> table = {0,  1,  2,  7,  3,  13, 8,  27, 4,  33, 14, 36, 9,  49, 28, 19,
                                             5,  25, 34, 17, 15, 53, 37, 55, 10, 46, 50, 39, 29, 42, 20, 57,
                                             63, 6,  12, 26, 32, 35, 48, 18, 24, 16, 52, 54, 45, 38, 41, 56,
                                             62, 11, 31, 47, 23, 51, 44, 40, 61, 30, 22, 43, 60, 21, 59, 58};
    return table[(x & (~x + 1)) * 0x218A7A392DD9ABF >> 58 & 0x3F];
#endif
}
#line 2 "utility/rep.cpp"
#include <algorithm>

class Range {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 3 "utility/revrep.cpp"

class ReversedRange {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr ReversedRange(const int first, const int last) noexcept
        : first(last - 1), last(std::min(first, last) - 1) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr ReversedRange revrep(const int l, const int r) noexcept { return ReversedRange(l, r); }
constexpr ReversedRange revrep(const int n) noexcept { return ReversedRange(0, n); }
#line 9 "container/dual_segment_tree.cpp"

template <class M> class DualSegmentTree {
    using T = typename M::Type;
    int internal_size, logn;
    std::vector<T> lazy;

    void apply(const int k, const T& e) { lazy[k] = M::operation(lazy[k], e); }
    void flush(const int k) {
        apply(2 * k, lazy[k]);
        apply(2 * k + 1, lazy[k]);
        lazy[k] = M::identity();
    }
    void push(const int k) {
        for (const int d : revrep(countr_zero(k) + 1, logn + 1)) flush(k >> d);
    }

  public:
    explicit DualSegmentTree(const int size = 0, const T& value = M::identity())
        : DualSegmentTree(std::vector<T>(size, value)) {}
    explicit DualSegmentTree(const std::vector<T>& vec) : internal_size(vec.size()) {
        logn = ceil_log2(internal_size);
        lazy = std::vector<T>(2 * internal_size, M::identity());
        for (const int i : rep(internal_size)) lazy[i] = vec[i];
    }

    int size() const { return internal_size; }

    void operate(int l, int r, const T& e) {
        assert(0 <= l and l <= r and r <= internal_size);
        l += internal_size;
        r += internal_size;
        push(l);
        push(r);
        while (l < r) {
            if (l & 1) apply(l++, e);
            if (r & 1) apply(--r, e);
            l >>= 1;
            r >>= 1;
        }
    }
    void assign(int i, const T& e) {
        assert(0 <= i and i < internal_size);
        i += internal_size;
        for (const int d : revrep(1, logn + 1)) flush(i >> d);
        lazy[i] = e;
    }

    T get(int i) const {
        assert(0 <= i and i < internal_size);
        i += internal_size;
        T ret = M::identity();
        while (i > 0) {
            ret = M::operation(ret, lazy[i]);
            i >>= 1;
        }
        return ret;
    }
};
Back to top page