proconlib

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

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: container/sparse_table.cpp

Depends on

Verified with

Code

#pragma once
#include <cassert>
#include <vector>
#include "../utility/bit_width.cpp"
#include "../utility/rep.cpp"

template <class M> class SparseTable {
    using T = typename M::Type;
    std::vector<std::vector<T>> table;

  public:
    SparseTable() : SparseTable(std::vector<T>()) {}
    explicit SparseTable(const std::vector<T>& vec) {
        const int size = vec.size();
        const int height = bit_width(size);
        table.reserve(height);
        table.push_back(vec);
        for (const int d : rep(1, height)) {
            table.push_back(std::vector<T>(size - (1 << d) + 1, M::identity()));
            for (const int i : rep(table[d].size())) {
                table[d][i] = M::operation(table[d - 1][i], table[d - 1][i + (1 << (d - 1))]);
            }
        }
    }

    int size() const { return table[0].size(); }

    T fold(const int l, const int r) const {
        assert(0 <= l and l <= r and r <= size());
        if (l == r) return M::identity();
        const int d = bit_width(r - l) - 1;
        return M::operation(table[d][l], table[d][r - (1 << d)]);
    }
};
#line 2 "container/sparse_table.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 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 6 "container/sparse_table.cpp"

template <class M> class SparseTable {
    using T = typename M::Type;
    std::vector<std::vector<T>> table;

  public:
    SparseTable() : SparseTable(std::vector<T>()) {}
    explicit SparseTable(const std::vector<T>& vec) {
        const int size = vec.size();
        const int height = bit_width(size);
        table.reserve(height);
        table.push_back(vec);
        for (const int d : rep(1, height)) {
            table.push_back(std::vector<T>(size - (1 << d) + 1, M::identity()));
            for (const int i : rep(table[d].size())) {
                table[d][i] = M::operation(table[d - 1][i], table[d - 1][i + (1 << (d - 1))]);
            }
        }
    }

    int size() const { return table[0].size(); }

    T fold(const int l, const int r) const {
        assert(0 <= l and l <= r and r <= size());
        if (l == r) return M::identity();
        const int d = bit_width(r - l) - 1;
        return M::operation(table[d][l], table[d][r - (1 << d)]);
    }
};
Back to top page