shuprog1's blog

By shuprog1, 10 years ago, In English

Please suggest an approach to solve this problem. (Problem Link)

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Firstly we have to convert the tree queries into array queries. We can perform a dfs and using start and end times , convert the tree to an array. Then each subtree is a range l to r in the array. Now according to the question we have to count the nodes in the subtree rooted at X with value between l to r. That is a range query in array which can be handled using segment tree.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    According to me, it was required to run BFS/DFS from node x and check the heights of all nodes in subtree to be in range [L,R]. I constructed a directed graph and followed this approach which gave me WA. Can you tell why this approach is wrong?