cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: sparse table
(include/emthrm/data_structure/sparse_table.hpp)

時間計算量

$\langle O(N\log{N}), O(1) \rangle$

仕様

template <typename Band>
struct SparseTable;

メンバ変数

名前 効果・戻り値
SparseTable(); デフォルトコンストラクタ
explicit SparseTable(const std::vector<Band>& a, const BinOp bin_op); $A$ に対して二項演算 $\mathrm{binOp}$ のオブジェクトを構築する。
void init(const std::vector<Band>& a, const BinOp bin_op_); $A$ によって初期化する。
Band query(const int left, const int right) const; $[\mathrm{left}, \mathrm{right})$ における演算を行った解

メンバ型

名前 効果・戻り値
BinOp std::function<Band(Band, Band)>

参考文献

Submissons

https://judge.yosupo.jp/submission/2719

Required by

Verified with

Code

#ifndef EMTHRM_DATA_STRUCTURE_SPARSE_TABLE_HPP_
#define EMTHRM_DATA_STRUCTURE_SPARSE_TABLE_HPP_

#include <algorithm>
#include <bit>
#include <cassert>
#include <functional>
#include <vector>

namespace emthrm {

template <typename Band>
struct SparseTable {
  using BinOp = std::function<Band(Band, Band)>;

  SparseTable() = default;

  explicit SparseTable(const std::vector<Band>& a, const BinOp bin_op) {
    init(a, bin_op);
  }

  void init(const std::vector<Band>& a, const BinOp bin_op_) {
    bin_op = bin_op_;
    const int n = a.size();
    assert(n > 0);
    lg.assign(n + 1, 0);
    for (int i = 2; i <= n; ++i) {
      lg[i] = lg[i >> 1] + 1;
    }
    const int table_h = std::countr_zero(std::bit_floor(a.size())) + 1;
    data.assign(table_h, std::vector<Band>(n));
    std::copy(a.begin(), a.end(), data.front().begin());
    for (int i = 1; i < table_h; ++i) {
      for (int j = 0; j + (1 << i) <= n; ++j) {
        data[i][j] = bin_op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
      }
    }
  }

  Band query(const int left, const int right) const {
    assert(left < right);
    const int h = lg[right - left];
    return bin_op(data[h][left], data[h][right - (1 << h)]);
  }

 private:
  BinOp bin_op;
  std::vector<int> lg;
  std::vector<std::vector<Band>> data;
};

}  // namespace emthrm

#endif  // EMTHRM_DATA_STRUCTURE_SPARSE_TABLE_HPP_
#line 1 "include/emthrm/data_structure/sparse_table.hpp"



#include <algorithm>
#include <bit>
#include <cassert>
#include <functional>
#include <vector>

namespace emthrm {

template <typename Band>
struct SparseTable {
  using BinOp = std::function<Band(Band, Band)>;

  SparseTable() = default;

  explicit SparseTable(const std::vector<Band>& a, const BinOp bin_op) {
    init(a, bin_op);
  }

  void init(const std::vector<Band>& a, const BinOp bin_op_) {
    bin_op = bin_op_;
    const int n = a.size();
    assert(n > 0);
    lg.assign(n + 1, 0);
    for (int i = 2; i <= n; ++i) {
      lg[i] = lg[i >> 1] + 1;
    }
    const int table_h = std::countr_zero(std::bit_floor(a.size())) + 1;
    data.assign(table_h, std::vector<Band>(n));
    std::copy(a.begin(), a.end(), data.front().begin());
    for (int i = 1; i < table_h; ++i) {
      for (int j = 0; j + (1 << i) <= n; ++j) {
        data[i][j] = bin_op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
      }
    }
  }

  Band query(const int left, const int right) const {
    assert(left < right);
    const int h = lg[right - left];
    return bin_op(data[h][left], data[h][right - (1 << h)]);
  }

 private:
  BinOp bin_op;
  std::vector<int> lg;
  std::vector<std::vector<Band>> data;
};

}  // namespace emthrm
Back to top page