Skip to content

Commit 1452e4f

Browse files
committed
linearize ladder array
1 parent 714a135 commit 1452e4f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

library/trees/ladder_decomposition/linear_kth_par.hpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
//! @space O(n)
1010
struct linear_kth_par {
1111
struct node {
12-
int d, p = -1, idx, dl;
13-
vi lad;
12+
int d, p = -1, dl, idx_j, idx_l;
1413
};
1514
vector<node> t;
1615
vector<pii> j;
16+
vi lad;
1717
linear_kth_par(const vector<vi>& adj):
1818
t(sz(adj)), j(2 * sz(t)) {
1919
vi st;
@@ -25,7 +25,7 @@ struct linear_kth_par {
2525
};
2626
auto dfs = [&](auto&& self, int v) -> void {
2727
st.push_back(v);
28-
t[v].idx = pos, t[v].dl = v;
28+
t[v].idx_j = pos, t[v].dl = v;
2929
add_j();
3030
for (int u : adj[v])
3131
if (u != t[v].p) {
@@ -40,12 +40,10 @@ struct linear_kth_par {
4040
rep(i, 0, sz(t)) {
4141
if (t[i].p == -1) dfs(dfs, i);
4242
if (t[i].p == -1 || t[t[i].p].dl != t[i].dl) {
43-
int leaf = t[i].dl;
44-
vi& lad = t[leaf].lad;
45-
lad.resize(
46-
min((t[leaf].d - t[i].d) * 2, t[leaf].d + 1),
47-
leaf);
48-
rep(k, 1, sz(lad)) lad[k] = t[lad[k - 1]].p;
43+
int v = t[i].dl, len = (t[v].d - t[i].d) * 2;
44+
t[v].idx_l = sz(lad) + t[v].d;
45+
for (; v != -1 && len--; v = t[v].p)
46+
lad.push_back(v);
4947
}
5048
}
5149
}
@@ -57,9 +55,9 @@ struct linear_kth_par {
5755
case 2: return t[t[v].p].p;
5856
default:
5957
int i = 1 << __lg(k / 3);
60-
auto [j1, j2] = j[(t[v].idx & -i) | i];
61-
int leaf = t[t[v].d - t[j2].d <= k ? j2 : j1].dl;
62-
return t[leaf].lad[k + t[leaf].d - t[v].d];
58+
auto [j1, j2] = j[(t[v].idx_j & -i) | i];
59+
int up = t[v].d - t[j2].d <= k ? j2 : j1;
60+
return lad[t[t[up].dl].idx_l + k - t[v].d];
6361
}
6462
}
6563
};

0 commit comments

Comments
 (0)