This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/datastructure/RangeTree.hpp"2 次元平面上の一点更新及び長方形領域のクエリに答えるデータ構造. 事前に座標圧縮等を施す必要はないが,一点更新が適用される点の候補を予め列挙しておく必要がある. 仕組みとしては,1 次元 Segment Tree の各ノードに対応する区間を再びデータ構造を用いて管理している.
以下に記す時間計算量において,$n$ は一点更新が適用される候補点の数である.空間計算量は $O(n \log n)$ となる.
| メンバ関数 | 効果 | 時間計算量 |
|---|---|---|
RangeTree(st_new, st_set, st_prod, op, e) |
サイズが与えられた際にデータ構造を構築する関数 st_new,各ノードのデータ構造を更新する関数 st_set,各ノードのデータ構造の区間クエリを取得する関数 st_prod,値を合成する関数 op 及び単位元 e の情報を与える. |
$O(1)$ |
add_point(x, y) |
候補点 $(x, y)$ を追加する. | $O(1)$ |
build() |
追加された候補点の情報をもとにデータ構造を構築する. | $O(n \log n)$ |
set(x, y, val) |
点 $(x, y)$ の値を $val$ をもとに更新する. | $O(n(\log n)^2)$ |
query(xl, xr, yl, yr) |
長方形領域 $[x_l, x_r) \times [y_l, y_r)$ のクエリに答える. | $O(n(\log n)^2)$ |
引数の渡し方がかなり複雑なのでテストファイルも併せて参考にするのが良い.
#pragma once
#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>
template <typename structure_t, typename value_t, typename coordinate_t> struct RangeTree {
private:
using structure_new = std::function<structure_t*(int)>;
using structure_set = std::function<void(structure_t&, int, value_t)>;
using structure_prod = std::function<value_t(structure_t&, int, int)>;
using value_merge = std::function<value_t(value_t, value_t)>;
using Pt = std::pair<coordinate_t, coordinate_t>;
public:
RangeTree(const structure_new& st_new,
const structure_set& st_set,
const structure_prod& st_prod,
const value_merge& op,
const value_t& e)
: st_new(st_new), st_set(st_set), st_prod(st_prod), op(op), e(e) {}
void add_point(coordinate_t x, coordinate_t y) { points.emplace_back(x, y); }
void build() {
std::sort(points.begin(), points.end());
points.erase(std::unique(points.begin(), points.end()), points.end());
n = points.size();
yxs.resize(n << 1);
segs.resize(n << 1, nullptr);
for (int i = 0; i < n; i++) yxs[n + i] = {{points[i].second, points[i].first}};
for (int i = n - 1; i >= 0; i--) {
auto& lch = yxs[i << 1 | 0];
auto& rch = yxs[i << 1 | 1];
std::merge(lch.begin(), lch.end(), rch.begin(), rch.end(), std::back_inserter(yxs[i]));
yxs[i].erase(std::unique(yxs[i].begin(), yxs[i].end()), yxs[i].end());
}
for (int i = 0; i < (n << 1); i++) segs[i] = st_new(yxs[i].size());
}
void set(coordinate_t x, coordinate_t y, value_t val) {
int i = std::distance(points.begin(), std::lower_bound(points.begin(), points.end(), std::make_pair(x, y)));
assert(i < n && points[i] == std::make_pair(x, y));
for (i += n; i; i >>= 1) st_set(*segs[i], zip(i, x, y), val);
}
value_t prod(coordinate_t xl, coordinate_t xr, coordinate_t yl, coordinate_t yr) {
assert(xl <= xr && yl <= yr);
value_t L = e, R = e;
int l = zip(xl), r = zip(xr);
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = op(L, st_prod(*segs[l], zip(l, yl), zip(l, yr))), l++;
if (r & 1) --r, R = op(st_prod(*segs[r], zip(r, yl), zip(r, yr)), R);
}
return op(L, R);
}
private:
int n;
const structure_new st_new;
const structure_set st_set;
const structure_prod st_prod;
const value_merge op;
const value_t e;
std::vector<Pt> points;
std::vector<std::vector<Pt>> yxs;
std::vector<structure_t*> segs;
int zip(coordinate_t x) {
auto compare = [](const Pt& l, const Pt& r) { return l.first < r.first; };
return std::distance(
points.begin(), std::lower_bound(points.begin(), points.end(), std::make_pair(x, coordinate_t()), compare));
}
int zip(int i, coordinate_t y) {
auto compare = [](const Pt& l, const Pt& r) { return l.first < r.first; };
return std::distance(
yxs[i].begin(), std::lower_bound(yxs[i].begin(), yxs[i].end(), std::make_pair(y, coordinate_t()), compare));
}
int zip(int i, coordinate_t x, coordinate_t y) {
return std::distance(yxs[i].begin(), std::lower_bound(yxs[i].begin(), yxs[i].end(), std::make_pair(y, x)));
}
};#line 2 "src/datastructure/RangeTree.hpp"
#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>
template <typename structure_t, typename value_t, typename coordinate_t> struct RangeTree {
private:
using structure_new = std::function<structure_t*(int)>;
using structure_set = std::function<void(structure_t&, int, value_t)>;
using structure_prod = std::function<value_t(structure_t&, int, int)>;
using value_merge = std::function<value_t(value_t, value_t)>;
using Pt = std::pair<coordinate_t, coordinate_t>;
public:
RangeTree(const structure_new& st_new,
const structure_set& st_set,
const structure_prod& st_prod,
const value_merge& op,
const value_t& e)
: st_new(st_new), st_set(st_set), st_prod(st_prod), op(op), e(e) {}
void add_point(coordinate_t x, coordinate_t y) { points.emplace_back(x, y); }
void build() {
std::sort(points.begin(), points.end());
points.erase(std::unique(points.begin(), points.end()), points.end());
n = points.size();
yxs.resize(n << 1);
segs.resize(n << 1, nullptr);
for (int i = 0; i < n; i++) yxs[n + i] = {{points[i].second, points[i].first}};
for (int i = n - 1; i >= 0; i--) {
auto& lch = yxs[i << 1 | 0];
auto& rch = yxs[i << 1 | 1];
std::merge(lch.begin(), lch.end(), rch.begin(), rch.end(), std::back_inserter(yxs[i]));
yxs[i].erase(std::unique(yxs[i].begin(), yxs[i].end()), yxs[i].end());
}
for (int i = 0; i < (n << 1); i++) segs[i] = st_new(yxs[i].size());
}
void set(coordinate_t x, coordinate_t y, value_t val) {
int i = std::distance(points.begin(), std::lower_bound(points.begin(), points.end(), std::make_pair(x, y)));
assert(i < n && points[i] == std::make_pair(x, y));
for (i += n; i; i >>= 1) st_set(*segs[i], zip(i, x, y), val);
}
value_t prod(coordinate_t xl, coordinate_t xr, coordinate_t yl, coordinate_t yr) {
assert(xl <= xr && yl <= yr);
value_t L = e, R = e;
int l = zip(xl), r = zip(xr);
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = op(L, st_prod(*segs[l], zip(l, yl), zip(l, yr))), l++;
if (r & 1) --r, R = op(st_prod(*segs[r], zip(r, yl), zip(r, yr)), R);
}
return op(L, R);
}
private:
int n;
const structure_new st_new;
const structure_set st_set;
const structure_prod st_prod;
const value_merge op;
const value_t e;
std::vector<Pt> points;
std::vector<std::vector<Pt>> yxs;
std::vector<structure_t*> segs;
int zip(coordinate_t x) {
auto compare = [](const Pt& l, const Pt& r) { return l.first < r.first; };
return std::distance(
points.begin(), std::lower_bound(points.begin(), points.end(), std::make_pair(x, coordinate_t()), compare));
}
int zip(int i, coordinate_t y) {
auto compare = [](const Pt& l, const Pt& r) { return l.first < r.first; };
return std::distance(
yxs[i].begin(), std::lower_bound(yxs[i].begin(), yxs[i].end(), std::make_pair(y, coordinate_t()), compare));
}
int zip(int i, coordinate_t x, coordinate_t y) {
return std::distance(yxs[i].begin(), std::lower_bound(yxs[i].begin(), yxs[i].end(), std::make_pair(y, x)));
}
};