Skip to content

Commit a5b5946

Browse files
authored
Merge pull request #376 from hitonanode/add-method-hilbert-order-mos
add simple mo method to hilbert order mos
2 parents b6d44d9 + a55c400 commit a5b5946

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

other_algorithms/hilbert_order_mos.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ std::vector<int> sort_by_hilbert_order(const std::vector<std::pair<Int, Int>> &p
5757
// Mo's algorithm with Hilbert order
5858
// - add(x, y) : Add (x, y) as query.
5959
// - run(IncX, DecX, IncY, DecY, Query) : run Mo's algorithm.
60+
// - run(Add, Remove, Query) : run Mo's algorithm with Add, Remove, and Query functions. add(x, y) means [x, y).
6061
struct MosAlgorithmHilbertOrder {
6162
int cx, cy;
6263
std::vector<std::pair<int, int>> queries;
@@ -79,4 +80,9 @@ struct MosAlgorithmHilbertOrder {
7980
query(q);
8081
}
8182
}
83+
84+
template <typename F1, typename F3, typename F5> void run(F1 Add, F3 Remove, F5 Query) {
85+
run([&](int x, int) { Remove(x); }, [&](int, int x) { Add(x); },
86+
[&](int y, int) { Add(y); }, [&](int, int y) { Remove(y); }, Query);
87+
}
8288
};

other_algorithms/hilbert_order_mos.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Mo's algorithm の一種で,2 次元平面上の点として表現可能なク
77

88
## 使用方法
99

10+
### 一般の 2 次元平面上の点クエリの場合
11+
1012
```cpp
1113
vector<Result> ret(Q); // 答えを格納する領域
1214
int x_init = 0, y_init = 0;
@@ -26,9 +28,25 @@ mo.run(inc_x, dec_x, inc_y, dec_y, solve);
2628
for (auto ans : ret) cout << ans << '\n';
2729
```
2830
31+
### 特に半開区間 $[l, r)$ クエリで区間の左右の伸張・収縮が同一の関数で書ける場合
32+
33+
```cpp
34+
MosAlgorithmHilbertOrder mos(0, 0);
35+
for (auto [l, r] : queries) {
36+
mos.add(l, r);
37+
}
38+
39+
mos.run(
40+
[&](int i) { /* Add i */ },
41+
[&](int i) { /* Remove i */ },
42+
[&](int q) { /* ret.at(q) = get_solution(); */ }
43+
);
44+
```
45+
2946
## 問題例
3047

3148
- [AtCoder Beginner Contest 384 G - Abs Sum](https://atcoder.jp/contests/abc384/tasks/abc384_g)
49+
- [AtCoder Beginner Contest 405 G - Range Shuffle Query](https://atcoder.jp/contests/abc405/tasks/abc405_g)
3250

3351
## Links
3452

0 commit comments

Comments
 (0)