-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcaRangeQuery.h
More file actions
151 lines (135 loc) · 3.97 KB
/
LcaRangeQuery.h
File metadata and controls
151 lines (135 loc) · 3.97 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#include <vector>
#include <functional>
#include <cmath>
#include "Graph/Graph.h"
#include "DataStructure/RangeQueries/SparseTable.h"
class LcaRangeQuery {
private:
std::vector<int> heights;
std::vector<int> pre;
SparseTable<int> st;
std::vector<int> euler;
public:
template<typename L>
LcaRangeQuery(const AdjList<L>& g, int root) : st({}) {
heights.resize(g.size());
pre.resize(g.size());
euler.resize(2 * g.size() - 1);
int index = 0;
std::function<void(int, int)> dfs = [&dfs, &g, &index, this](int u, int height) {
heights[u] = height;
pre[u] = index;
euler[index++] = u;
for (Edge<L> v : g.adj[u]) {
dfs(v.to, height + 1);
euler[index++] = u;
}
};
dfs(root, 0);
st = SparseTable<int>(euler, [this](int a, int b) { return heights[a] < heights[b] ? a : b; });
}
int query(int a, int b) {
if (pre[a] > pre[b])
std::swap(a, b);
return st.query(pre[a], pre[b]);
}
};
// Farach-Colton and Bender Algorithm
class LcaRangeQueryOptimized {
private:
std::vector<int> heights;
std::vector<int> pre;
std::vector<int> euler;
SparseTable<int> st;
int blockSize, blockCount;
std::vector<int> blockMask;
std::vector<std::vector<std::vector<int>>> blocks;
int minHeight(int a, int b) {
return heights[a] < heights[b] ? a : b;
}
void buildSparseTable() {
std::vector<int> eulerBlocks(blockCount);
for (int block = 0; block < blockCount; ++block) {
int blockStart = block * blockSize;
eulerBlocks[block] = euler[blockStart];
for (int i = 1; i < blockSize && blockStart + i < int(euler.size()); ++i) {
eulerBlocks[block] = minHeight(eulerBlocks[block], euler[blockStart + i]);
}
}
st = SparseTable<int>(eulerBlocks, [this](int a, int b) { return minHeight(a, b); });
}
void buildMasks() {
blockMask.resize(blockCount);
for (int block = 0; block < blockCount; ++block) {
int mask = 0;
int blockStart = block * blockSize;
for (int i = 0; i < blockSize - 1; ++i) {
if (blockStart + i >= blockCount || // Last block
heights[euler[blockStart + i + 1]] - heights[euler[blockStart + i]] == 1) {
mask += (1 << i);
}
}
blockMask[block] = mask;
}
blocks.assign(1 << (blockSize - 1), std::vector<std::vector<int>>(blockSize, std::vector<int>(blockSize)));
for (int mask = 0; mask < (1 << (blockSize - 1)); ++mask) {
for (int l = 0; l < blockSize; ++l) {
int value = 0;
int minValue = value;
int minIndex = l;
blocks[mask][l][l] = minIndex;
for (int r = l + 1; r < blockSize; ++r) {
value += (mask & (1 << (r - 1))) ? 1 : -1;
if (value < minValue) {
minValue = value;
minIndex = r;
}
blocks[mask][l][r] = minIndex;
}
}
}
}
int minQueryInBlock(int block, int l, int r) {
return blocks[blockMask[block]][l][r] + block * blockSize;
}
public:
template<typename L>
LcaRangeQueryOptimized(const AdjList<L>& g, int root) : st({}) {
heights.resize(g.size());
pre.resize(g.size());
euler.resize(2 * g.size() - 1);
int index = 0;
std::function<void(int, int)> dfs = [&dfs, &g, &index, this](int u, int height) {
heights[u] = height;
pre[u] = index;
euler[index++] = u;
for (Edge<L> v : g.adj[u]) {
dfs(v.to, height + 1);
euler[index++] = u;
}
};
dfs(root, 0);
blockSize = std::max(1, int(log2(euler.size()) / 2));
blockCount = (int(euler.size()) - 1) / blockSize + 1;
buildSparseTable();
buildMasks();
}
int query(int a, int b) {
if (pre[a] > pre[b])
std::swap(a, b);
int l = pre[a], r = pre[b];
int lBlock = l / blockSize, rBlock = r / blockSize;
int lInBlock = l % blockSize, rInBlock = r % blockSize;
if (lBlock == rBlock) {
return euler[minQueryInBlock(lBlock, lInBlock, rInBlock)];
} else {
int prefixMin = euler[minQueryInBlock(lBlock, lInBlock, blockSize - 1)];
int suffixMin = euler[minQueryInBlock(rBlock, 0, rInBlock)];
int lca = minHeight(prefixMin, suffixMin);
if (lBlock + 1 < rBlock)
lca = minHeight(lca, st.query(lBlock + 1, rBlock - 1));
return lca;
}
}
};