Skip to content

rectangle_add_point_get.hpp

SECTIONData Structure INCLUDEnoya/rectangle_add_point_get.hpp

对多个轴对齐矩形整体加权,并查询指定点累计受到的贡献;适合矩形覆盖、点询问模型。

Complexity: Time: O(r log^2 r) preprocessing and O(log^2 r) per rectangle addition or point query; the batch helper is O((R + Q) log(R + Q)). Space: O(r log r) for the dynamic structure and O(R + Q) for the helper.

AC 记录:rectangle_add_point_get

跳到代码 · GitHub ↗

Implementation

当前头文件,省略 include guard;依赖见 #include

/// @complexity Time: O(r log^2 r) preprocessing and O(log^2 r) per rectangle
/// addition or point query; the batch helper is O((R + Q) log(R + Q)).
/// Space: O(r log r) for the dynamic structure and O(R + Q) for the helper.

#include "noya/rectangle_sum.hpp"
#include "noya/sparse_fenwick_2d.hpp"

#include <array>
#include <vector>

namespace noya {

/// @brief Rectangle-add point-get data structure on sparse coordinates.
/// A half-open rectangle update is its four-corner two-dimensional difference;
/// the value at a point is the inclusive prefix sum of those differences.
/// Register all rectangles before build(), including rectangles added later.
template <class T, class Coordinate = int> struct rectangle_add_point_get_tree {
  sparse_fenwick_2d<T, Coordinate> bit;

  void reserve_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                         Coordinate up) {
    bit.reserve(L, dn);
    bit.reserve(L, up);
    bit.reserve(R, dn);
    bit.reserve(R, up);
  }

  void build() { bit.build(); }

  void add_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                     Coordinate up, const T &dif) {
    bit.add(L, dn, dif);
    bit.add(L, up, -dif);
    bit.add(R, dn, -dif);
    bit.add(R, up, dif);
  }

  T point_get(Coordinate x, Coordinate y) const {
    return bit.prefix_sum_inclusive(x, y);
  }
};

/// @brief Offline rectangle-add point-get queries.
/// Add weight w to [l, r) x [d, u), then return the value at every point.
template <class T, class C = noya::fenwick<T>>
std::vector<T>
rectangle_add_point_get(std::vector<std::array<int, 5>> rs,
                        std::vector<std::array<int, 2>> pt) {
  std::vector<std::array<int, 3>> pts;
  for (auto &[l, r, d, u, w] : rs) {
    pts.push_back({l, d, w});
    pts.push_back({l, u, -w});
    pts.push_back({r, d, -w});
    pts.push_back({r, u, w});
  }
  std::vector<std::array<int, 4>> qs;
  for (auto &[x, y] : pt) {
    qs.push_back({0, x + 1, 0, y + 1});
  }
  return rectangle_sum<T, C>(pts, qs);
}

} // namespace noya
#ifndef NOYA_RECTANGLE_ADD_POINT_GET_HPP
#define NOYA_RECTANGLE_ADD_POINT_GET_HPP 1

/// @complexity Time: O(r log^2 r) preprocessing and O(log^2 r) per rectangle
/// addition or point query; the batch helper is O((R + Q) log(R + Q)).
/// Space: O(r log r) for the dynamic structure and O(R + Q) for the helper.

#include "noya/rectangle_sum.hpp"
#include "noya/sparse_fenwick_2d.hpp"

#include <array>
#include <vector>

namespace noya {

/// @brief Rectangle-add point-get data structure on sparse coordinates.
/// A half-open rectangle update is its four-corner two-dimensional difference;
/// the value at a point is the inclusive prefix sum of those differences.
/// Register all rectangles before build(), including rectangles added later.
template <class T, class Coordinate = int> struct rectangle_add_point_get_tree {
  sparse_fenwick_2d<T, Coordinate> bit;

  void reserve_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                         Coordinate up) {
    bit.reserve(L, dn);
    bit.reserve(L, up);
    bit.reserve(R, dn);
    bit.reserve(R, up);
  }

  void build() { bit.build(); }

  void add_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                     Coordinate up, const T &dif) {
    bit.add(L, dn, dif);
    bit.add(L, up, -dif);
    bit.add(R, dn, -dif);
    bit.add(R, up, dif);
  }

  T point_get(Coordinate x, Coordinate y) const {
    return bit.prefix_sum_inclusive(x, y);
  }
};

/// @brief Offline rectangle-add point-get queries.
/// Add weight w to [l, r) x [d, u), then return the value at every point.
template <class T, class C = noya::fenwick<T>>
std::vector<T>
rectangle_add_point_get(std::vector<std::array<int, 5>> rs,
                        std::vector<std::array<int, 2>> pt) {
  std::vector<std::array<int, 3>> pts;
  for (auto &[l, r, d, u, w] : rs) {
    pts.push_back({l, d, w});
    pts.push_back({l, u, -w});
    pts.push_back({r, d, -w});
    pts.push_back({r, u, w});
  }
  std::vector<std::array<int, 4>> qs;
  for (auto &[x, y] : pt) {
    qs.push_back({0, x + 1, 0, y + 1});
  }
  return rectangle_sum<T, C>(pts, qs);
}

} // namespace noya

#endif // NOYA_RECTANGLE_ADD_POINT_GET_HPP
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <numeric>
#include <type_traits>
#include <utility>
#include <vector>

/// @complexity Time: O(r log^2 r) preprocessing and O(log^2 r) per rectangle
/// addition or point query; the batch helper is O((R + Q) log(R + Q)).
/// Space: O(r log r) for the dynamic structure and O(R + Q) for the helper.

/// @complexity Time: O((P + Q) log P) offline.
/// Space: O(P + Q).

/// @complexity Time: O(log n) point update or range sum.
/// Space: O(n).

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder

namespace noya {
template <class T> struct block {
  int V, B;

  block() {}
  block(const int &_V) {
    if (_V > 0) {
      build(_V);
    }
  }

  std::vector<T> pt, blo;
  void build(const int &_V) {
    V = _V;
    B = sqrt(V);
    pt.assign(V, 0);
    blo.assign(V / B + 1, 0);
  }

  void add(int x, T v) {
    assert(0 <= x && x < V);
    int bel = x / B;
    blo[bel] += v;
    pt[x] += v;
  }

  T query(int x) const {
    assert(0 <= x && x <= V);
    T res = 0;
    int bel = x / B;
    for (int i = 0; i < bel; i++)
      res += blo[i];
    int st = bel * B;
    int end = x;
    for (int i = st; i < end; i++)
      res += pt[i];
    return res;
  }

  /// @brief Sum of [l, r).
  T prod(int l, int r) const {
    assert(0 <= l && l <= r && r <= V);
    return query(r) - query(l);
  }
};

template <class T> struct fenwick : atcoder::fenwick_tree<T> {
  using atcoder::fenwick_tree<T>::fenwick_tree;
  using atcoder::fenwick_tree<T>::add;
  T query(int x) { return this->sum(0, x); }
  T prod(int l, int r) { return this->sum(l, r); }
};

} // namespace noya

namespace noya {

/// @brief Offline rectangle sum. Points (x, y, weight), queries [l, r) x [d, u).
template <class T, class C = noya::fenwick<T>>
std::vector<T> rectangle_sum(std::vector<std::array<int, 3>> pt,
                             std::vector<std::array<int, 4>> qs) {
  std::vector<int> Xs;
  std::vector<int> Ys;
  for (auto &[x, y, z] : pt) {
    Xs.push_back(x);
    Ys.push_back(y);
  }
  for (auto &[l, r, d, u] : qs) {
    Xs.push_back(l);
    Xs.push_back(r);
    Ys.push_back(d);
    Ys.push_back(u);
  }

  std::sort(Xs.begin(), Xs.end());
  Xs.erase(std::unique(Xs.begin(), Xs.end()), Xs.end());

  std::sort(Ys.begin(), Ys.end());
  Ys.erase(std::unique(Ys.begin(), Ys.end()), Ys.end());

  for (auto &[x, y, z] : pt) {
    x = std::lower_bound(Xs.begin(), Xs.end(), x) - Xs.begin();
    y = std::lower_bound(Ys.begin(), Ys.end(), y) - Ys.begin();
  }

  for (auto &[l, r, d, u] : qs) {
    l = std::lower_bound(Xs.begin(), Xs.end(), l) - Xs.begin();
    r = std::lower_bound(Xs.begin(), Xs.end(), r) - Xs.begin();
    d = std::lower_bound(Ys.begin(), Ys.end(), d) - Ys.begin();
    u = std::lower_bound(Ys.begin(), Ys.end(), u) - Ys.begin();
  }

  int X = int(Xs.size());
  int Y = int(Ys.size());
  C F(Y);
  std::vector<std::vector<std::array<int, 3>>> Q(X + 1);
  for (int i = 0; i < int(qs.size()); i++) {
    auto &[l, r, d, u] = qs[i];
    Q[r].push_back({i, d, u});
    Q[l].push_back({~i, d, u});
  }

  std::vector<std::vector<std::array<int, 2>>> A(X + 1);
  for (auto &[x, y, z] : pt) {
    A[x].push_back({y, z});
  }

  std::vector<T> ans(qs.size());
  for (int x = 0; x < X; x++) {
    for (auto &[y, z] : A[x]) {
      F.add(y, z);
    }
    for (auto &[i, d, u] : Q[x + 1]) {
      if (i >= 0)
        ans[i] += F.prod(d, u);
      else
        ans[~i] -= F.prod(d, u);
    }
  }
  return ans;
}

} // namespace noya

/// @complexity Time: O(p log^2 p) registration/build and O(log^2 p) per point
/// addition, prefix sum, or rectangle sum.  Space: O(p log p).

namespace noya {

/// @brief Offline-built sparse two-dimensional Fenwick tree.
/// Register every coordinate that may later be updated, then call build().
/// An x-Fenwick node stores only the registered y-coordinates contributing to
/// it, so the dense coordinate plane is replaced by O(p log p) cells.
template <class T, class Coordinate = int> struct sparse_fenwick_2d {
  std::vector<std::pair<Coordinate, Coordinate>> pt;
  std::vector<Coordinate> xs;
  std::vector<std::vector<Coordinate>> ys;
  std::vector<std::vector<T>> dat;
  bool ok = false;

  /// @brief Remove all registered coordinates and values.
  void clear() {
    pt.clear();
    xs.clear();
    ys.clear();
    dat.clear();
    ok = false;
  }

  /// @brief Register a coordinate that may be passed to add() after build().
  void reserve(Coordinate x, Coordinate y) {
    assert(!ok);
    pt.emplace_back(x, y);
  }

  /// @brief Build the compressed Fenwick nodes with every value initially zero.
  void build() {
    assert(!ok);
    xs.reserve(pt.size());
    for (auto [x, y] : pt) {
      (void)y;
      xs.push_back(x);
    }
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
    ys.assign(xs.size() + 1, {});
    for (auto [x, y] : pt) {
      int idx = int(std::lower_bound(xs.begin(), xs.end(), x) - xs.begin());
      for (idx++; idx <= int(xs.size()); idx += idx & -idx) {
        ys[idx].push_back(y);
      }
    }
    dat.resize(xs.size() + 1);
    for (int idx = 1; idx <= int(xs.size()); idx++) {
      auto &xs0 = ys[idx];
      std::sort(xs0.begin(), xs0.end());
      xs0.erase(std::unique(xs0.begin(), xs0.end()), xs0.end());
      dat[idx].assign(xs0.size() + 1, T{});
    }
    pt.clear();
    pt.shrink_to_fit();
    ok = true;
  }

  /// @brief Add delta at a coordinate registered before build().
  void add(Coordinate x, Coordinate y, const T &dif) {
    assert(ok);
    auto ix = std::lower_bound(xs.begin(), xs.end(), x);
    assert(ix != xs.end() && *ix == x);
    int xi = int(ix - xs.begin()) + 1;
    for (; xi <= int(xs.size()); xi += xi & -xi) {
      auto iy = std::lower_bound(ys[xi].begin(), ys[xi].end(), y);
      assert(iy != ys[xi].end() && *iy == y);
      int yi = int(iy - ys[xi].begin()) + 1;
      for (; yi < int(dat[xi].size()); yi += yi & -yi) {
        dat[xi][yi] += dif;
      }
    }
  }

  /// @brief Sum values at coordinates (px, py) with px < x and py < y.
  T prefix_sum(Coordinate x, Coordinate y) const {
    assert(ok);
    int xi = int(std::lower_bound(xs.begin(), xs.end(), x) - xs.begin());
    T res{};
    for (; xi > 0; xi -= xi & -xi) {
      int yi = int(std::lower_bound(ys[xi].begin(), ys[xi].end(), y) -
                   ys[xi].begin());
      for (; yi > 0; yi -= yi & -yi) {
        res += dat[xi][yi];
      }
    }
    return res;
  }

  /// @brief Sum values at coordinates (px, py) with px <= x and py <= y.
  T prefix_sum_inclusive(Coordinate x, Coordinate y) const {
    assert(ok);
    int xi = int(std::upper_bound(xs.begin(), xs.end(), x) - xs.begin());
    T res{};
    for (; xi > 0; xi -= xi & -xi) {
      int yi = int(std::upper_bound(ys[xi].begin(), ys[xi].end(), y) -
                   ys[xi].begin());
      for (; yi > 0; yi -= yi & -yi) {
        res += dat[xi][yi];
      }
    }
    return res;
  }

  /// @brief Sum values in [l, r) x [dn, up).
  T rectangle_sum(Coordinate l, Coordinate r, Coordinate dn,
                  Coordinate up) const {
    return prefix_sum(r, up) - prefix_sum(l, up) - prefix_sum(r, dn) +
           prefix_sum(l, dn);
  }
};

} // namespace noya

namespace noya {

/// @brief Rectangle-add point-get data structure on sparse coordinates.
/// A half-open rectangle update is its four-corner two-dimensional difference;
/// the value at a point is the inclusive prefix sum of those differences.
/// Register all rectangles before build(), including rectangles added later.
template <class T, class Coordinate = int> struct rectangle_add_point_get_tree {
  sparse_fenwick_2d<T, Coordinate> bit;

  void reserve_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                         Coordinate up) {
    bit.reserve(L, dn);
    bit.reserve(L, up);
    bit.reserve(R, dn);
    bit.reserve(R, up);
  }

  void build() { bit.build(); }

  void add_rectangle(Coordinate L, Coordinate dn, Coordinate R,
                     Coordinate up, const T &dif) {
    bit.add(L, dn, dif);
    bit.add(L, up, -dif);
    bit.add(R, dn, -dif);
    bit.add(R, up, dif);
  }

  T point_get(Coordinate x, Coordinate y) const {
    return bit.prefix_sum_inclusive(x, y);
  }
};

/// @brief Offline rectangle-add point-get queries.
/// Add weight w to [l, r) x [d, u), then return the value at every point.
template <class T, class C = noya::fenwick<T>>
std::vector<T>
rectangle_add_point_get(std::vector<std::array<int, 5>> rs,
                        std::vector<std::array<int, 2>> pt) {
  std::vector<std::array<int, 3>> pts;
  for (auto &[l, r, d, u, w] : rs) {
    pts.push_back({l, d, w});
    pts.push_back({l, u, -w});
    pts.push_back({r, d, -w});
    pts.push_back({r, u, w});
  }
  std::vector<std::array<int, 4>> qs;
  for (auto &[x, y] : pt) {
    qs.push_back({0, x + 1, 0, y + 1});
  }
  return rectangle_sum<T, C>(pts, qs);
}

} // namespace noya