Skip to content

rectangle_sum.hpp

SECTIONData Structure INCLUDEnoya/ds/rectangle_sum.hpp

对静态带权点集回答轴对齐矩形内权值和;适合所有点预先给出、随后只有矩形查询的题目。

Complexity: Time: \(O((P + Q) \log (P + Q))\) with the default Fenwick backend. Custom \(C\) follows its construction, add, and prod costs. Space: \(O(P + Q)\).

AC 记录:rectangle_sum。

跳到代码 · GitHub ↗

Implementation

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

/// @complexity Time: O((P + Q) log(P + Q)) with the default Fenwick backend.
/// Custom C follows its construction, add, and prod costs.
/// Space: O(P + Q).

#include "noya/ds/point_add_range_sum.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <vector>

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<T> ans(qs.size());
  if (pt.empty() || qs.empty()) {
    return ans;
  }
  std::sort(pt.begin(), pt.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  std::vector<int> ys;
  ys.reserve(pt.size());
  for (auto [x, y, w] : pt) {
    ys.push_back(y);
  }
  std::sort(ys.begin(), ys.end());
  ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
  for (auto &[x, y, w] : pt) {
    y = int(std::lower_bound(ys.begin(), ys.end(), y) - ys.begin());
  }

  std::vector<std::array<int, 4>> events;
  events.reserve(qs.size() * 2);
  for (int i = 0; i < int(qs.size()); ++i) {
    auto [l, r, d, u] = qs[i];
    d = int(std::lower_bound(ys.begin(), ys.end(), d) - ys.begin());
    u = int(std::lower_bound(ys.begin(), ys.end(), u) - ys.begin());
    if (l == r || d == u) continue;
    events.push_back({l, d, u, ~i});
    events.push_back({r, d, u, i});
  }
  if (events.empty()) {
    return ans;
  }
  std::sort(events.begin(), events.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  C bit(int(ys.size()));
  std::size_t p = 0;
  for (auto [x, d, u, idx] : events) {
    // Strictly smaller x preserves both half-open rectangle boundaries.
    while (p < pt.size() && pt[p][0] < x) {
      bit.add(pt[p][1], T(pt[p][2]));
      ++p;
    }
    if (idx >= 0)
      ans[idx] += bit.prod(d, u);
    else
      ans[~idx] -= bit.prod(d, u);
  }
  return ans;
}

} // namespace noya

 // NOYA_RECTANGLE_SUM_HPP
#ifndef NOYA_RECTANGLE_SUM_HPP
#define NOYA_RECTANGLE_SUM_HPP 1

/// @complexity Time: O((P + Q) log(P + Q)) with the default Fenwick backend.
/// Custom C follows its construction, add, and prod costs.
/// Space: O(P + Q).

#include "noya/ds/point_add_range_sum.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <vector>

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<T> ans(qs.size());
  if (pt.empty() || qs.empty()) {
    return ans;
  }
  std::sort(pt.begin(), pt.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  std::vector<int> ys;
  ys.reserve(pt.size());
  for (auto [x, y, w] : pt) {
    ys.push_back(y);
  }
  std::sort(ys.begin(), ys.end());
  ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
  for (auto &[x, y, w] : pt) {
    y = int(std::lower_bound(ys.begin(), ys.end(), y) - ys.begin());
  }

  std::vector<std::array<int, 4>> events;
  events.reserve(qs.size() * 2);
  for (int i = 0; i < int(qs.size()); ++i) {
    auto [l, r, d, u] = qs[i];
    d = int(std::lower_bound(ys.begin(), ys.end(), d) - ys.begin());
    u = int(std::lower_bound(ys.begin(), ys.end(), u) - ys.begin());
    if (l == r || d == u) continue;
    events.push_back({l, d, u, ~i});
    events.push_back({r, d, u, i});
  }
  if (events.empty()) {
    return ans;
  }
  std::sort(events.begin(), events.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  C bit(int(ys.size()));
  std::size_t p = 0;
  for (auto [x, d, u, idx] : events) {
    // Strictly smaller x preserves both half-open rectangle boundaries.
    while (p < pt.size() && pt[p][0] < x) {
      bit.add(pt[p][1], T(pt[p][2]));
      ++p;
    }
    if (idx >= 0)
      ans[idx] += bit.prod(d, u);
    else
      ans[~idx] -= bit.prod(d, u);
  }
  return ans;
}

} // namespace noya

#endif // NOYA_RECTANGLE_SUM_HPP
/// @complexity Time: O((P + Q) log(P + Q)) with the default Fenwick backend.
/// Custom C follows its construction, add, and prod costs.
/// Space: O(P + Q).

/// @complexity Time: fenwick O(log n) update/query; block O(1) update and
/// O(sqrt(n)) query. Both initialize in O(n).
/// Space: O(n).

#include <cassert>
#include <vector>

#include <cassert>
#include <numeric>
#include <type_traits>

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <vector>

namespace noya {
/// @brief Square-root decomposition for point additions and range sums.
template <class T> struct block {
  int V = 0, B = 1;

  block() = default;
  block(const int &_V) { build(_V); }

  std::vector<T> pt, blo;
  void build(const int &_V) {
    assert(_V >= 0);
    V = _V;
    B = std::max(1, int(std::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);
    if (l == 0) {
      return query(r);
    }
    int lb = l / B, rb = r / B;
    T res = 0;
    if (lb == rb) {
      for (int i = l; i < r; ++i)
        res += pt[i];
      return res;
    }
    if (l == lb * B) {
      res += blo[lb];
    } else {
      for (int i = l; i < (lb + 1) * B; ++i)
        res += pt[i];
    }
    for (int i = lb + 1; i < rb; ++i)
      res += blo[i];
    for (int i = rb * B; i < r; ++i)
      res += pt[i];
    return res;
  }
};

/// @brief Fenwick-tree wrapper with prefix query and half-open prod aliases.
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

#include <algorithm>
#include <array>
#include <cstddef>
#include <vector>

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<T> ans(qs.size());
  if (pt.empty() || qs.empty()) {
    return ans;
  }
  std::sort(pt.begin(), pt.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  std::vector<int> ys;
  ys.reserve(pt.size());
  for (auto [x, y, w] : pt) {
    ys.push_back(y);
  }
  std::sort(ys.begin(), ys.end());
  ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
  for (auto &[x, y, w] : pt) {
    y = int(std::lower_bound(ys.begin(), ys.end(), y) - ys.begin());
  }

  std::vector<std::array<int, 4>> events;
  events.reserve(qs.size() * 2);
  for (int i = 0; i < int(qs.size()); ++i) {
    auto [l, r, d, u] = qs[i];
    d = int(std::lower_bound(ys.begin(), ys.end(), d) - ys.begin());
    u = int(std::lower_bound(ys.begin(), ys.end(), u) - ys.begin());
    if (l == r || d == u) continue;
    events.push_back({l, d, u, ~i});
    events.push_back({r, d, u, i});
  }
  if (events.empty()) {
    return ans;
  }
  std::sort(events.begin(), events.end(),
            [](const auto &a, const auto &b) { return a[0] < b[0]; });
  C bit(int(ys.size()));
  std::size_t p = 0;
  for (auto [x, d, u, idx] : events) {
    // Strictly smaller x preserves both half-open rectangle boundaries.
    while (p < pt.size() && pt[p][0] < x) {
      bit.add(pt[p][1], T(pt[p][2]));
      ++p;
    }
    if (idx >= 0)
      ans[idx] += bit.prod(d, u);
    else
      ans[~idx] -= bit.prod(d, u);
  }
  return ans;
}

} // namespace noya

 // NOYA_RECTANGLE_SUM_HPP