KatInterview

Batch Simulation of Conflict-Free Node Locks on a Rooted Tree

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designtreeeuler-tourfenwick-tree

Last updated

Process a batch of lock, unlock and query operations on a rooted tree where no locked node may be an ancestor or descendant of another, using subtree ranges and Fenwick trees.

You are given a rooted tree with n nodes labeled 0 to n - 1. The tree is described by an array parent of length n, where parent[0] = -1 (node 0 is the root) and parent[i] is the parent of node i for every i > 0.

Every node starts out unlocked. You must simulate a sequence of operations and report the result of each one. The operations are given as two parallel lists: operations[j] is the name of the j-th operation and arguments[j] is a list whose first element arguments[j][0] is the node the operation targets.

The supported operations are:

  • "lock": Locks node x and returns true only if all of the following hold: x itself is not locked, no ancestor of x is locked, and no descendant of x is locked. Otherwise nothing changes and it returns false.
  • "unlock": If x is currently locked, unlocks it and returns true. Otherwise nothing changes and it returns false.
  • "isLocked": Returns true if x is currently locked, false otherwise. It never changes any state.

Here, "ancestor" means any node on the path from the parent of x up to the root, and "descendant" means any node in the subtree of x other than x itself. Siblings and cousins never conflict with each other.

Return a list of booleans, one per operation, in the same order as the operations.

The operations are meant to be handled efficiently, so a solution that walks the whole path to the root or the whole subtree for every operation may be too slow on large inputs.

Constraints

  • 1 <= n <= 2 * 10^5
  • parent[0] = -1, and for i > 0, 0 <= parent[i] < n; the array describes a valid tree rooted at 0.
  • The number of operations is at most 2 * 10^5.
  • len(operations) == len(arguments), and each arguments[j] contains a valid node label in position 0.
  • Each operation name is one of "lock", "unlock", "isLocked".
  • The tree can be a long chain, so deep recursion may be a problem.

Examples

Example 1

Input: simulate_tree_locks([-1, 0, 0, 1, 1], ['lock', 'isLocked', 'lock', 'unlock', 'lock', 'isLocked'], [[0], [3], [3], [0], [1], [3]])

Output: [True, False, False, True, True, False]

Example 2

Input: simulate_tree_locks([-1, 0, 1], ['lock', 'lock', 'unlock', 'lock'], [[2], [1], [2], [1]])

Output: [True, False, True, True]

Hints

Hint 1

Flatten the tree with a DFS so that every subtree occupies one contiguous range of positions. Do it iteratively, since the tree can be 200000 levels deep.

Hint 2

Asking whether any descendant (or the node itself) is locked becomes a range-sum query over the subtree's range, which a Fenwick tree handles in O(log n).

Hint 3

To detect a locked ancestor, when locking y add +1 over y's whole subtree range using a difference array stored in a second Fenwick tree. A point query at x then counts the locked nodes whose subtree contains x. Undo the same updates on unlock.