I have a tree. I want to answer queries such as:
- Add value on a path
- Get sum on a path
I am using Heavy-Light Decomposition. There are n
nodes in the tree and m
queries. With HLD, when I know the Lowest Common Ancestor, I can separate any query for u
and v
into two distinct ones: from u
to lca
and from v
to lca
. As a result, the query will be answered in O(log^2n)
time (log
to go up from u
and v
to lca
, and log
for segment trees on heavy paths).
As you know, HLD is built in O(n)
time, as a result, total time is O(n + m*log^2n)
. My question is how to find LCA using already built HLD. I cannot invent the algorithm.
I can use binary climb to get LCA, but it takes O(nlogn)
preprocessing, which will make asymptotic behaviour worse. I can also use Range Minimum Query, which doesn't spoil the time, but I'd like to use HLD for this procedure. Thank you for any ideas!