Kitty's Calculations on a Tree

  • + 0 comments

    I was able to find some useful hints in the discussions, but they're buried below a mountain of misinformation and nonfunctional solutions. So if you're looking for help, here are the basic components of the solution (or at least, of a solution).

    1. Use a log(n) least-common-ancestor algorithm such as binary lifting.
    2. Order the nodes of the full tree using a post-order traversal.
    3. Process the nodes for each individual query using a priority-queue with the aformentioned node ordering.
    4. Each term in the summation you are computing can be broken into two terms by considering the lowest-common-ancestor of the two nodes. Using this fact, you can precompute a few values for any pair of subtrees that allow you to compute their combined sum in constant time.