This problem can be solved with BIT or Merge Sort Tree after constructing the required array using DFS (topological sort). The main observation is that for a node LL it's range for the given query is L+sub_tree_size[L]L+sub\_tree\_size[L]. and you can get the exact node's order by doing a topological sort in the tree.

Then you can do basic merge sort tree to find the answer where the time complexity is Q×log2(n)Q \times log^2(n) or you can do it with BIT where the time complexity is Q×log(n)Q \times log(n).

Statistics

92% Solution Ratio
fsshakkhorEarliest, Apr '19
mahabub.tweetFastest, 0.0s
mdshadeshLightest, 81 kB
SoudipShortest, 723B
Toph uses cookies. By continuing you agree to our Cookie Policy.