Can anyone explain the approach to solve problem PERSISTENT OAK(OAK) from the June Long Challenge 17 ?
Thanks!
Can anyone explain the approach to solve problem PERSISTENT OAK(OAK) from the June Long Challenge 17 ?
Thanks!
I did persistent segment tree with Heavy-Light Decomposition, with a range copy functionality. We do range min to look for branch to cut, and range copy form state[0] to perform the cut.
I used two trees for this: a range-min tree of acorn capacity (per HLD chain) and a tree based on the DFS-order of the chains. Let’s call the former the mintree and the latter the oaktree.
The mintree contains the capacity of a branch. Initially, the value at a branch u is W[u]. Note: a negative value of the mintree means that a branch can’t hold capacity, so it is cut.
The oaktree is a meta segment tree of the mintree. This is built in DFS-order (aka Euler Tour) of the chains. For example, if node u lives in chain[u], we can store the whole subtree of chain[u] located in some range [L_{chain[u]}, R_{chain[u]}] in the tree. Here, L_{chain[u]} refers to the current chain, and the value there is the root of the persistent mintree at chain[u]. Chains below will be at the range [L_{chain[u]} + 1, R_{chain[u]}].
Query 1: Add x acorns to branch u.
Query 2: Bird sits on branch u.
For query 1, accessing a mintree is O(log N). There is one mintree per chain and there are O(log N) chains so doing both becomes O(log^2 N). For query 2, we only replace O(log N) nodes during range copy and we only access a single mintree. Total runtime is O(Q log^2 N).
Remember to do everything persistently, so make sure every update needs to return a new tree. Hope that was helpful. Here’s link to solution.