Блог пользователя rpthegreat

Автор rpthegreat, история, 7 лет назад, По-английски

Hello, Given a tree (nodes and their weight) and query which contains two number node (of tree guaranteed )and value c . I need to find number of ancestors which has weight greater than c. I saved query and sorted according to their query value. I stored graph node and their respective weight in 2-D array and sorted according to their weight. I set bit array initially all zero and start visiting query array from last. Now how to use segment tree for query. Do i need to use another concept ? please help me . Thank you.

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Ok, first what you need to do is represent the tree as array (using in-order). For every vertex you will have from[v] and to[v] (the range in the array that is the subtree of v) .Then put all the vertices and the queries in the same array and sort them in non-increasing way. When you are at a vertex you must make seg[from[v]]+=1 and seg[to[v]+1]-=1 . Now when you are facing a query you must simply output the sum [1,from[v]]

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

First make a list of queries for every node, then do a DFS keeping an order-statistic tree, like a treap or the policy-based data structures from the STL. When you enter a node, you add the value to the tree, when you exit the node you remove the value. To answer a query, just find the order of the first key greater than c in the tree.

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

You can do one dfs keeping all ancestors of current node in an order statistics tree keyed by node weight. Answering queries for current node is just counting number of elements in the order statistics tree with value > c