rectangle_add_point_get.hpp¶
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.
Verified by rectangle_add_point_get.
对多个轴对齐矩形整体加权,并查询指定点累计受到的贡献;适合矩形覆盖、点询问模型。
Implementation¶
#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> differences;
void reserve_rectangle(Coordinate left, Coordinate down, Coordinate right,
Coordinate up) {
differences.reserve(left, down);
differences.reserve(left, up);
differences.reserve(right, down);
differences.reserve(right, up);
}
void build() { differences.build(); }
void add_rectangle(Coordinate left, Coordinate down, Coordinate right,
Coordinate up, const T &delta) {
differences.add(left, down, delta);
differences.add(left, up, -delta);
differences.add(right, down, -delta);
differences.add(right, up, delta);
}
T point_get(Coordinate x, Coordinate y) const {
return differences.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>> areas,
std::vector<std::array<int, 2>> points) {
std::vector<std::array<int, 3>> weighted_points;
for (auto &[l, r, d, u, w] : areas) {
weighted_points.push_back({l, d, w});
weighted_points.push_back({l, u, -w});
weighted_points.push_back({r, d, -w});
weighted_points.push_back({r, u, w});
}
std::vector<std::array<int, 4>> queries;
for (auto &[x, y] : points) {
queries.push_back({0, x + 1, 0, y + 1});
}
return rectangle_sum<T, C>(weighted_points, queries);
}
} // 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, sqrtV;
block() {}
block(const int &_V) {
if (_V > 0) {
build(_V);
}
}
std::vector<T> point, blo;
void build(const int &_V) {
V = _V;
sqrtV = sqrt(V);
point.assign(V, 0);
blo.assign(V / sqrtV + 1, 0);
}
void add(int x, T v) {
assert(0 <= x && x < V);
int bel = x / sqrtV;
blo[bel] += v;
point[x] += v;
}
T query(int x) const {
assert(0 <= x && x <= V);
T res = 0;
int bel = x / sqrtV;
for (int i = 0; i < bel; i++)
res += blo[i];
int start = bel * sqrtV;
int end = x;
for (int i = start; i < end; i++)
res += point[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>> points,
std::vector<std::array<int, 4>> queries) {
std::vector<int> Xs;
std::vector<int> Ys;
for (auto &[x, y, z] : points) {
Xs.push_back(x);
Ys.push_back(y);
}
for (auto &[l, r, d, u] : queries) {
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] : points) {
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] : queries) {
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(queries.size()); i++) {
auto &[l, r, d, u] = queries[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] : points) {
A[x].push_back({y, z});
}
std::vector<T> ans(queries.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>> registered;
std::vector<Coordinate> xs;
std::vector<std::vector<Coordinate>> ys;
std::vector<std::vector<T>> data;
bool built = false;
/// @brief Remove all registered coordinates and values.
void clear() {
registered.clear();
xs.clear();
ys.clear();
data.clear();
built = false;
}
/// @brief Register a coordinate that may be passed to add() after build().
void reserve(Coordinate x, Coordinate y) {
assert(!built);
registered.emplace_back(x, y);
}
/// @brief Build the compressed Fenwick nodes with every value initially zero.
void build() {
assert(!built);
xs.reserve(registered.size());
for (auto [x, y] : registered) {
(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] : registered) {
int index = int(std::lower_bound(xs.begin(), xs.end(), x) - xs.begin());
for (index++; index <= int(xs.size()); index += index & -index) {
ys[index].push_back(y);
}
}
data.resize(xs.size() + 1);
for (int index = 1; index <= int(xs.size()); index++) {
auto &coordinates = ys[index];
std::sort(coordinates.begin(), coordinates.end());
coordinates.erase(std::unique(coordinates.begin(), coordinates.end()),
coordinates.end());
data[index].assign(coordinates.size() + 1, T{});
}
registered.clear();
registered.shrink_to_fit();
built = true;
}
/// @brief Add delta at a coordinate registered before build().
void add(Coordinate x, Coordinate y, const T &delta) {
assert(built);
auto x_iterator = std::lower_bound(xs.begin(), xs.end(), x);
assert(x_iterator != xs.end() && *x_iterator == x);
int x_index = int(x_iterator - xs.begin()) + 1;
for (; x_index <= int(xs.size()); x_index += x_index & -x_index) {
auto y_iterator = std::lower_bound(ys[x_index].begin(), ys[x_index].end(), y);
assert(y_iterator != ys[x_index].end() && *y_iterator == y);
int y_index = int(y_iterator - ys[x_index].begin()) + 1;
for (; y_index < int(data[x_index].size()); y_index += y_index & -y_index) {
data[x_index][y_index] += delta;
}
}
}
/// @brief Sum values at coordinates (px, py) with px < x and py < y.
T prefix_sum(Coordinate x, Coordinate y) const {
assert(built);
int x_index = int(std::lower_bound(xs.begin(), xs.end(), x) - xs.begin());
T result{};
for (; x_index > 0; x_index -= x_index & -x_index) {
int y_index = int(std::lower_bound(ys[x_index].begin(), ys[x_index].end(), y) -
ys[x_index].begin());
for (; y_index > 0; y_index -= y_index & -y_index) {
result += data[x_index][y_index];
}
}
return result;
}
/// @brief Sum values at coordinates (px, py) with px <= x and py <= y.
T prefix_sum_inclusive(Coordinate x, Coordinate y) const {
assert(built);
int x_index = int(std::upper_bound(xs.begin(), xs.end(), x) - xs.begin());
T result{};
for (; x_index > 0; x_index -= x_index & -x_index) {
int y_index = int(std::upper_bound(ys[x_index].begin(), ys[x_index].end(), y) -
ys[x_index].begin());
for (; y_index > 0; y_index -= y_index & -y_index) {
result += data[x_index][y_index];
}
}
return result;
}
/// @brief Sum values in [left, right) x [down, up).
T rectangle_sum(Coordinate left, Coordinate right, Coordinate down,
Coordinate up) const {
return prefix_sum(right, up) - prefix_sum(left, up) -
prefix_sum(right, down) + prefix_sum(left, down);
}
};
} // 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> differences;
void reserve_rectangle(Coordinate left, Coordinate down, Coordinate right,
Coordinate up) {
differences.reserve(left, down);
differences.reserve(left, up);
differences.reserve(right, down);
differences.reserve(right, up);
}
void build() { differences.build(); }
void add_rectangle(Coordinate left, Coordinate down, Coordinate right,
Coordinate up, const T &delta) {
differences.add(left, down, delta);
differences.add(left, up, -delta);
differences.add(right, down, -delta);
differences.add(right, up, delta);
}
T point_get(Coordinate x, Coordinate y) const {
return differences.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>> areas,
std::vector<std::array<int, 2>> points) {
std::vector<std::array<int, 3>> weighted_points;
for (auto &[l, r, d, u, w] : areas) {
weighted_points.push_back({l, d, w});
weighted_points.push_back({l, u, -w});
weighted_points.push_back({r, d, -w});
weighted_points.push_back({r, u, w});
}
std::vector<std::array<int, 4>> queries;
for (auto &[x, y] : points) {
queries.push_back({0, x + 1, 0, y + 1});
}
return rectangle_sum<T, C>(weighted_points, queries);
}
} // namespace noya