cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 2次元 Fenwick tree
(include/emthrm/data_structure/fenwick_tree/2d_fenwick_tree.hpp)

Fenwick tree (binary indexed tree)

時間計算量

  時間計算量
Fenwick tree $\langle O(N), O(\log{N}) \rangle$
2次元 Fenwick tree $\langle O(HW), O((\log{H})(\log{W})) \rangle$

仕様

Fenwick tree

template <typename Abelian>
struct FenwickTree;

メンバ関数

名前 効果・戻り値 要件
explicit FenwickTree(const int n, const Abelian ID = 0); 要素数 $N$、単位元 $\mathrm{id}$ のオブジェクトを構築する。  
void add(int idx, const Abelian val); $A_{\mathrm{idx}} \gets A_{\mathrm{idx}} + \mathrm{val}$  
Abelian sum(int idx) const; $\sum_{i = 0}^{\mathrm{idx} - 1} A_i$  
Abelian sum(const int left, const int right) const; $\sum_{i = \mathrm{left}}^{\mathrm{right} - 1} A_i$  
Abelian operator[](const int idx) const; $A_{\mathrm{idx}}$  
int lower_bound(Abelian val) const; $\min \lbrace\,k \mid \sum_{i = 0}^k A_i \geq \mathrm{val} \rbrace$ $A_i \geq (\text{単位元})$ ($i = 0,\ldots, N - 1$)

区間加算クエリ対応 Fenwick tree

template <typename Abelian>
struct FenwickTreeSupportingRangeAddQuery;

メンバ関数

名前 効果・戻り値
explicit FenwickTreeSupportingRangeAddQuery(const int n_, const Abelian ID = 0); 要素数 $N$、単位元 $\mathrm{id}$ のオブジェクトを構築する。
void add(int left, const int right, const Abelian val); $A_i \gets A_i + \mathrm{val}$ ($i = \mathrm{left},\ldots, \mathrm{right} - 1$)
Abelian sum(const int idx) const; $\sum_{i = 0}^{\mathrm{idx} - 1} A_i$
Abelian sum(const int left, const int right) const; $\sum_{i = \mathrm{left}}^{\mathrm{right} - 1} A_i$
Abelian operator[](const int idx) const; $A_{\mathrm{idx}}$

2次元 Fenwick tree

template <typename Abelian>
struct FenwickTreeSupportingRangeAddQuery;

メンバ関数

名前 効果・戻り値
explicit FenwickTree2D(const int height_, const int width_, const Abelian ID = 0); 要素数 $\mathrm{height} \times \mathrm{width}$、単位元 $\mathrm{id}$ のオブジェクトを構築する。
void add(int y, int x, const Abelian val); $A_{yx} \gets A_{yx} + \mathrm{val}$
Abelian sum(int y, int x) const; $\sum_{i = 0}^y \sum_{j = 0}^x A_{ij}$
Abelian sum(const int y1, const int x1, const int y2, const int x2) const; $\sum_{i = y_1}^{y_2} \sum_{j = x_1}^{x_2} A_{ij}$
Abelian get(const int y, const int x) const; $A_{yx}$

区間加算クエリ対応2次元 Fenwick tree

template <typename Abelian>
struct FenwickTree2DSupportingRangeAddQuery;

メンバ関数

名前 効果・戻り値
explicit FenwickTree2DSupportingRangeAddQuery(const int height_, const int width_, const Abelian ID = 0); 要素数 $\mathrm{height} \times \mathrm{width}$、単位元 $\mathrm{id}$ のオブジェクトを構築する。
void add(int y1, int x1, int y2, int x2, const Abelian val); $A_{ij} \gets A_{ij} + \mathrm{val}$ ($y_1 \leq i \leq y_2,\ x_1 \leq j \leq x_2$)
Abelian sum(int y, int x) const; $\sum_{i = 0}^y \sum_{j = 0}^x A_{ij}$
Abelian sum(const int y1, const int x1, const int y2, const int x2) const; $\sum_{i = y_1}^{y_2} \sum_{j = x_1}^{x_2} A_{ij}$

区間加算クエリ対応2次元 Fenwick tree の実装

$A_{ij} \gets A_{ij} + v$ ($y_1 \leq i \leq y_2,\ x_1 \leq j \leq x_2$) を考える。

$S \mathrel{:=} \sum_{i = 1}^y \sum_{j = 1}^x A_{ij}$ とおき、加算前の $S$ を $S_b$、加算後の $S$ を $S_a$ とすると

参考文献

TODO

Submissons

Verified with

Code

#ifndef EMTHRM_DATA_STRUCTURE_FENWICK_TREE_2D_FENWICK_TREE_HPP_
#define EMTHRM_DATA_STRUCTURE_FENWICK_TREE_2D_FENWICK_TREE_HPP_

#include <vector>

namespace emthrm {

template <typename Abelian>
struct FenwickTree2D {
  explicit FenwickTree2D(
      const int height_, const int width_, const Abelian ID = 0)
      : height(height_ + 1), width(width_ + 1), ID(ID) {
    data.assign(height, std::vector<Abelian>(width, ID));
  }

  void add(int y, int x, const Abelian val) {
    ++y; ++x;
    for (int i = y; i < height; i += i & -i) {
      for (int j = x; j < width; j += j & -j) {
        data[i][j] += val;
      }
    }
  }

  Abelian sum(int y, int x) const {
    ++y; ++x;
    Abelian res = ID;
    for (int i = y; i > 0; i -= i & -i) {
      for (int j = x; j > 0; j -= j & -j) {
        res += data[i][j];
      }
    }
    return res;
  }

  Abelian sum(const int y1, const int x1, const int y2, const int x2) const {
    return y1 > y2 || x1 > x2 ? ID : sum(y2, x2) - sum(y2, x1 - 1)
                                     - sum(y1 - 1, x2) + sum(y1 - 1, x1 - 1);
  }

  Abelian get(const int y, const int x) const { return sum(y, x, y, x); }

 private:
  const int height, width;
  const Abelian ID;
  std::vector<std::vector<Abelian>> data;
};

}  // namespace emthrm

#endif  // EMTHRM_DATA_STRUCTURE_FENWICK_TREE_2D_FENWICK_TREE_HPP_
#line 1 "include/emthrm/data_structure/fenwick_tree/2d_fenwick_tree.hpp"



#include <vector>

namespace emthrm {

template <typename Abelian>
struct FenwickTree2D {
  explicit FenwickTree2D(
      const int height_, const int width_, const Abelian ID = 0)
      : height(height_ + 1), width(width_ + 1), ID(ID) {
    data.assign(height, std::vector<Abelian>(width, ID));
  }

  void add(int y, int x, const Abelian val) {
    ++y; ++x;
    for (int i = y; i < height; i += i & -i) {
      for (int j = x; j < width; j += j & -j) {
        data[i][j] += val;
      }
    }
  }

  Abelian sum(int y, int x) const {
    ++y; ++x;
    Abelian res = ID;
    for (int i = y; i > 0; i -= i & -i) {
      for (int j = x; j > 0; j -= j & -j) {
        res += data[i][j];
      }
    }
    return res;
  }

  Abelian sum(const int y1, const int x1, const int y2, const int x2) const {
    return y1 > y2 || x1 > x2 ? ID : sum(y2, x2) - sum(y2, x1 - 1)
                                     - sum(y1 - 1, x2) + sum(y1 - 1, x1 - 1);
  }

  Abelian get(const int y, const int x) const { return sum(y, x, y, x); }

 private:
  const int height, width;
  const Abelian ID;
  std::vector<std::vector<Abelian>> data;
};

}  // namespace emthrm
Back to top page