-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mo's algorithm.cpp
58 lines (47 loc) · 1.26 KB
/
Mo's algorithm.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
template <typename T, typename Tans>
struct Mo {
struct Query {
int l, r, idx, block;
Query(int _l, int _r, int _idx, int _block)
: l(_l),
r(_r),
idx(_idx),
block(_block) {}
bool operator<(const Query &q) const {
if (block != q.block)
return block < q.block;
return (block & 1 ? (r < q.r) : (r > q.r));
}
};
vector<T> vs;
vector<Query> qs;
const int block_size;
Mo(const vector<T> &a)
: vs(a),
block_size((int)ceil(sqrt(a.size()))) {}
void add_query(int l, int r) {
qs.emplace_back(l, r, qs.size(),
l / block_size);
}
auto solve() {
// get answer return type
vector<Tans> answers(qs.size());
sort(all(qs));
int cur_l = 0, cur_r = -1;
for (auto q : qs) {
while (cur_l > q.l) add(--cur_l);
while (cur_r < q.r) add(++cur_r);
while (cur_l < q.l) remove(cur_l++);
while (cur_r > q.r) remove(cur_r--);
answers[q.idx] = get_answer();
}
return answers;
}
private:
// add value at idx from data structure
inline void add(int idx) {}
// remove value at idx from data structure
inline void remove(int idx) {}
// extract current answer of the data structure
inline Tans get_answer() {}
};