-
Notifications
You must be signed in to change notification settings - Fork 0
/
mo.hpp
39 lines (38 loc) · 1.73 KB
/
mo.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
// Mo's Algorithm
// https://snuke.hatenablog.com/entry/2016/07/01/000000
// complexity: O(N^2/B + QB)
// -> O(N sqrt(Q)) (B := N / sqrt(Q))
template <class AddLeft, class AddRight, class DelLeft, class DelRight, class Out> //
void mo(const int &n, const std::vector<int> &l, const std::vector<int> &r, //
const AddLeft &add_left, const AddRight &add_right, //
const DelLeft &del_left, const DelRight &del_right, const Out &out) {
const int q = (int)l.size();
if (q == 0) return;
// normal
// const int bucket_size = std::max(1.0, n / std::max(1.0, sqrt(q)));
// speed up by https://nyaannyaan.github.io/library/misc/mo.hpp
const int bucket_size = std::max(1.0, n / std::max(1.0, sqrt(2.0 * q / 3.0)));
std::vector<int> ind(q), lbs(q);
// reduce the number of divisions by memoization
for (int i = 0; i < q; i++) lbs[i] = l[i] / bucket_size;
std::iota(ind.begin(), ind.end(), 0);
std::sort(ind.begin(), ind.end(), [&](int i, int j) {
if (lbs[i] != lbs[j]) return l[i] < l[j];
return (lbs[i] & 1) ? r[i] > r[j] : r[i] < r[j];
});
// initialize with [ l[ind[0]], l[ind[0]] ) instead of [0, 0)
int now_l = l[ind[0]], now_r = now_l;
for (auto &&i : ind) {
while (now_l > l[i]) add_left(--now_l);
while (now_r < r[i]) add_right(now_r++);
while (now_l < l[i]) del_left(now_l++);
while (now_r > r[i]) del_right(--now_r);
out(i);
}
}
template <class Add, class Del, class Out> //
void mo(const int n, const std::vector<int> &l, const std::vector<int> &r, //
const Add &add, const Del &del, const Out &out) {
mo(n, l, r, add, add, del, del, out);
}