Luzhiled's Library

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

View the Project on GitHub ei1333/library

:question: Abstract 2D Binary Indexed Tree Compressed(抽象化2次元座圧BIT)
(structure/others/abstract-2d-binary-indexed-tree-compressed.hpp)

Depends on

Verified with

Code

#include "abstract-binary-indexed-tree.hpp"

/**
 * @brief Abstract 2D Binary Indexed Tree Compressed(抽象化2次元座圧BIT)
 */
template< typename T, typename F >
struct Abstract2DBinaryIndexedTreeCompressed {
private:
  int n;
  vector< AbstractBinaryIndexedTree< T, F > > data;
  const F f;
  const T e;
  vector< int > hs;
  vector< vector< int > > beet;
public:
  Abstract2DBinaryIndexedTreeCompressed(const vector< int > &hs, const F f, const T &e) :
      n((int) hs.size()), hs(hs), f(f), e(e) {
    vector< int > ord(n);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      return hs[a] < hs[b];
    });
    beet.resize(n + 1);
    for(auto &&i: ord) {
      for(int k = i + 1; k <= n; k += k & -k) {
        beet[k].emplace_back(hs[i]);
      }
    }
    data.reserve(n + 1);
    for(int k = 0; k <= n; k++) {
      beet[k].erase(unique(begin(beet[k]), end(beet[k])), end(beet[k]));
      data.emplace_back((int) beet[k].size(), f, e);
    }
  }

  void apply(int k1, const T &x) {
    int k2 = hs[k1];
    for(++k1; k1 <= n; k1 += k1 & -k1) {
      int p = lower_bound(begin(beet[k1]), end(beet[k1]), k2) - begin(beet[k1]);
      data[k1].apply(p, x);
    }
  }

  T prod(int r1, int r2) const {
    T ret{e};
    for(; r1 > 0; r1 -= r1 & -r1) {
      int p = lower_bound(begin(beet[r1]), end(beet[r1]), r2) - begin(beet[r1]);
      ret = f(ret, data[r1].prod(p));
    }
    return ret;
  }
};

template< typename T, typename F >
Abstract2DBinaryIndexedTreeCompressed< T, F > get_abstract_2d_binary_indexed_tree_compressed(const vector< int > &hs, const F &f, const T &e) {
  return Abstract2DBinaryIndexedTreeCompressed{hs, f, e};
}
#line 1 "structure/others/abstract-binary-indexed-tree.hpp"
/**
 * @brief Abstract Binary Indexed Tree(抽象化BIT)
 */
template< typename T, typename F >
struct AbstractBinaryIndexedTree {
private:
  int n;
  vector< T > data;
  const F f;
  const T e;

public:
  AbstractBinaryIndexedTree() = default;

  explicit AbstractBinaryIndexedTree(int n, const F f, const T &e) : n(n), f(f), e(e) {
    data.assign(n + 1, e);
  }

  explicit AbstractBinaryIndexedTree(const vector< T > &v, const F f, const T &e) :
      AbstractBinaryIndexedTree((int) v.size(), f, e) {
    build(v);
  }

  void build(const vector< T > &v) {
    assert(n == (int) v.size());
    for(int i = 1; i <= n; i++) data[i] = v[i - 1];
    for(int i = 1; i <= n; i++) {
      int j = i + (i & -i);
      if(j <= n) data[j] = f(data[j], data[i]);
    }
  }

  void apply(int k, const T &x) {
    for(++k; k <= n; k += k & -k) data[k] = f(data[k], x);
  }

  T prod(int r) const {
    T ret{e};
    for(; r > 0; r -= r & -r) ret = f(ret, data[r]);
    return ret;
  }
};

template< typename T, typename F >
AbstractBinaryIndexedTree< T, F > get_abstract_binary_indexed_tree(int n, const F &f, const T &e) {
  return AbstractBinaryIndexedTree{n, f, e};
}

template< typename T, typename F >
AbstractBinaryIndexedTree< T, F > get_abstract_binary_indexed_tree(const vector< T > &v, const F &f, const T &e) {
  return AbstractBinaryIndexedTree{v, f, e};
}
#line 2 "structure/others/abstract-2d-binary-indexed-tree-compressed.hpp"

/**
 * @brief Abstract 2D Binary Indexed Tree Compressed(抽象化2次元座圧BIT)
 */
template< typename T, typename F >
struct Abstract2DBinaryIndexedTreeCompressed {
private:
  int n;
  vector< AbstractBinaryIndexedTree< T, F > > data;
  const F f;
  const T e;
  vector< int > hs;
  vector< vector< int > > beet;
public:
  Abstract2DBinaryIndexedTreeCompressed(const vector< int > &hs, const F f, const T &e) :
      n((int) hs.size()), hs(hs), f(f), e(e) {
    vector< int > ord(n);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      return hs[a] < hs[b];
    });
    beet.resize(n + 1);
    for(auto &&i: ord) {
      for(int k = i + 1; k <= n; k += k & -k) {
        beet[k].emplace_back(hs[i]);
      }
    }
    data.reserve(n + 1);
    for(int k = 0; k <= n; k++) {
      beet[k].erase(unique(begin(beet[k]), end(beet[k])), end(beet[k]));
      data.emplace_back((int) beet[k].size(), f, e);
    }
  }

  void apply(int k1, const T &x) {
    int k2 = hs[k1];
    for(++k1; k1 <= n; k1 += k1 & -k1) {
      int p = lower_bound(begin(beet[k1]), end(beet[k1]), k2) - begin(beet[k1]);
      data[k1].apply(p, x);
    }
  }

  T prod(int r1, int r2) const {
    T ret{e};
    for(; r1 > 0; r1 -= r1 & -r1) {
      int p = lower_bound(begin(beet[r1]), end(beet[r1]), r2) - begin(beet[r1]);
      ret = f(ret, data[r1].prod(p));
    }
    return ret;
  }
};

template< typename T, typename F >
Abstract2DBinaryIndexedTreeCompressed< T, F > get_abstract_2d_binary_indexed_tree_compressed(const vector< int > &hs, const F &f, const T &e) {
  return Abstract2DBinaryIndexedTreeCompressed{hs, f, e};
}
Back to top page