Skip to content
Snippets Groups Projects
Commit f6e37529 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Small fixes and improvements

parent ef4d3297
No related branches found
No related tags found
No related merge requests found
......@@ -88,14 +88,14 @@ struct HLD {
for (; head[a] != head[b]; b = par[head[b]]) {
if (dep[head[a]] > dep[head[b]])
swap(a, b);
ans = seg.op(ans, seg.query(pos[head[b]], pos[b]));
ans = seg.func(ans, seg.query(pos[head[b]], pos[b]));
}
if (dep[a] > dep[b])
swap(a, b);
// Remove "+ 1" when values are associated with vertices
return seg.op(ans, seg.query(pos[a] + 1, pos[b]));
return seg.func(ans, seg.query(pos[a] + 1, pos[b]));
}
// Updates value of i-th edge (or vertice)
......
......@@ -22,10 +22,12 @@
vector<ii> graph[MAX];
int par[MAX][LOG], cost[MAX][LOG];
template <class F = function<T(const T&, const &T)>>
struct LCA {
F func;
vector<int> h;
LCA(int N) : h(N)
LCA(int N, F &func) : h(N), func(func)
{ init(); }
void init() {
......@@ -34,10 +36,6 @@ struct LCA {
dfs(0); // root is 0
}
int op(int a, int b) {
return a + b; // or max(a, b)
}
void dfs(int v, int p = -1, int c = 0) {
par[v][0] = p;
cost[v][0] = c;
......@@ -48,7 +46,7 @@ struct LCA {
for (int i = 1; i < LOG; ++i)
if (par[v][i - 1] != -1) {
par[v][i] = par[par[v][i - 1]][i - 1];
cost[v][i] = op(cost[v][i], op(cost[v][i-1],
cost[v][i] = func(cost[v][i], func(cost[v][i-1],
cost[par[v][i-1]][i-1]));
}
......@@ -65,7 +63,7 @@ struct LCA {
for (int i = LOG - 1; i >= 0; --i)
if (par[a][i] != -1 && h[par[a][i]] >= h[b]) {
ans = op(ans, cost[a][i]);
ans = func(ans, cost[a][i]);
a = par[a][i];
}
......@@ -79,7 +77,7 @@ struct LCA {
for (int i = LOG - 1; i >= 0; --i)
if (par[a][i] != -1 && par[a][i] != par[b][i]) {
ans = op(ans, op(cost[a][i], cost[b][i]));
ans = func(ans, func(cost[a][i], cost[b][i]));
a = par[a][i];
b = par[b][i];
}
......@@ -88,7 +86,7 @@ struct LCA {
if (a == b)
return ans;
else
return op(ans, op(cost[a][0], cost[b][0]));
return func(ans, func(cost[a][0], cost[b][0]));
#else
return par[a][0];
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment