Water Tree

Source: Codeforces 343D

  1. Read the problem and summarise the task requirements.

Let’s attempt some simplified versions of the problem.

  1. Suppose there are no operations of type 2, so the only operations are filling a subtree and querying a vertex. How would you solve this problem?
  2. Suppose there are no operations of type 1, so the only operations are emptying a node (and its ancestors) and querying a vertex. How would you solve this problem? Assume that the tree starts filled.

Let’s return to the original problem.

  1. Now that we can track when a node has been filled or emptied, we still need a way to determine which one happened most recently. What additional information can we store to help us with this?

  2. Design an algorithm to solve the problem.

  3. Analyse the time complexity of your algorithm and estimate its running time.

  4. Implement this algorithm in code.
  5. Submit your program for judging!