Skip to content

Commit e25fbe7

Browse files
lrvideckisweb-flow
andauthored
Kth par improvement (#81)
* better constant * [auto-verifier] verify commit c8b7231 --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 1b89bbc commit e25fbe7

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"tests/library_checker_aizu_tests/handmade_tests/dsu_size.test.cpp": "2024-09-22 18:42:16 -0500",
7171
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2024-09-21 13:04:45 -0500",
7272
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2024-09-23 11:20:46 -0500",
73-
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-10-07 00:04:04 -0700",
73+
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-10-16 11:17:12 -0700",
7474
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2024-10-06 21:59:37 -0700",
7575
"tests/library_checker_aizu_tests/handmade_tests/merge_sort_trees.test.cpp": "2024-09-22 18:42:16 -0500",
7676
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2024-09-19 12:57:45 -0700",
@@ -138,8 +138,8 @@
138138
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_update.test.cpp": "2024-09-22 18:42:16 -0500",
139139
"tests/library_checker_aizu_tests/trees/edge_cd_count_paths_per_length.test.cpp": "2024-09-21 13:04:45 -0500",
140140
"tests/library_checker_aizu_tests/trees/edge_cd_reroot_dp.test.cpp": "2024-09-21 13:04:45 -0500",
141-
"tests/library_checker_aizu_tests/trees/kth_node_on_path.test.cpp": "2024-10-07 00:04:04 -0700",
142-
"tests/library_checker_aizu_tests/trees/ladder_decomposition.test.cpp": "2024-09-27 10:44:49 -0500",
141+
"tests/library_checker_aizu_tests/trees/kth_node_on_path.test.cpp": "2024-10-16 11:17:12 -0700",
142+
"tests/library_checker_aizu_tests/trees/ladder_decomposition.test.cpp": "2024-10-16 11:17:12 -0700",
143143
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2024-10-07 00:04:04 -0700",
144144
"tests/library_checker_aizu_tests/trees/lca_all_methods_lib_checker.test.cpp": "2024-10-07 00:04:04 -0700",
145145
"tests/library_checker_aizu_tests/trees/subtree_isomorphism.test.cpp": "2024-09-19 12:57:45 -0700"

library/trees/ladder_decomposition/linear_kth_par.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
//! int kth_par = kp.kth_par(v, k);
66
//! @endcode
77
//! kth_par = a node k edges up from v
8-
//! @time O(3.5*n + q)
9-
//! @space O(3.5*n)
8+
//! @time O(n + q)
9+
//! @space O(n)
1010
struct linear_kth_par {
1111
struct node {
1212
int d, p = -1, idx, dl;
1313
vi lad;
1414
};
1515
vector<node> t;
16-
vi j;
16+
vector<pii> j;
1717
linear_kth_par(const vector<vi>& adj):
1818
t(sz(adj)), j(2 * sz(t)) {
1919
vi st;
2020
int pos = 1;
2121
auto add_j = [&]() -> void {
22-
j[pos] = st[max(0, sz(st) - 1 - 2 * (pos & -pos))];
22+
j[pos] = {st[max(0, sz(st) - 1 - 2 * (pos & -pos))],
23+
st[max(0, sz(st) - 1 - 4 * (pos & -pos))]};
2324
pos++;
2425
};
2526
auto dfs = [&](auto&& self, int v) -> void {
@@ -37,26 +38,27 @@ struct linear_kth_par {
3738
}
3839
st.pop_back();
3940
};
40-
rep(i, 0, sz(t)) if (t[i].p == -1) t[i].p = i,
41-
dfs(dfs, i);
41+
rep(i, 0, sz(t)) if (t[i].p == -1) dfs(dfs, i);
4242
rep(i, 0, sz(t)) if (
43-
t[i].p == i || t[t[i].p].dl != t[i].dl) {
43+
t[i].p == -1 || t[t[i].p].dl != t[i].dl) {
4444
int leaf = t[i].dl;
4545
vi& lad = t[leaf].lad;
4646
lad.resize(
47-
min((t[leaf].d - t[i].d) * 7 / 2, t[leaf].d + 1),
47+
min((t[leaf].d - t[i].d) * 2, t[leaf].d + 1),
4848
leaf);
4949
rep(k, 1, sz(lad)) lad[k] = t[lad[k - 1]].p;
5050
}
5151
}
5252
int kth_par(int v, int k) {
53+
assert(0 <= k && k <= t[v].d);
5354
switch (k) {
5455
case 0: return v;
5556
case 1: return t[v].p;
5657
case 2: return t[t[v].p].p;
5758
default:
58-
int i = 1 << __lg(k / 3),
59-
leaf = t[j[(t[v].idx & -i) | i]].dl;
59+
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;
6062
return t[leaf].lad[k + t[leaf].d - t[v].d];
6163
}
6264
}

0 commit comments

Comments
 (0)