CF85C.Petya and Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

One night, having had a hard day at work, Petya saw a nightmare. There was a binary search tree in the dream. But it was not the actual tree that scared Petya. The horrifying thing was that Petya couldn't search for elements in this tree. Petya tried many times to choose key and look for it in the tree, and each time he arrived at a wrong place. Petya has been racking his brains for long, choosing keys many times, but the result was no better. But the moment before Petya would start to despair, he had an epiphany: every time he was looking for keys, the tree didn't have the key, and occured exactly one mistake. "That's not a problem!", thought Petya. "Why not count the expectation value of an element, which is found when I search for the key". The moment he was about to do just that, however, Petya suddenly woke up.

Thus, you are given a binary search tree, that is a tree containing some number written in the node. This number is called the node key. The number of children of every node of the tree is equal either to 00 or to 22 . The nodes that have 00 children are called leaves and the nodes that have 22 children, are called inner. An inner node has the left child, that is the child whose key is less than the current node's key, and the right child, whose key is more than the current node's key. Also, a key of any node is strictly larger than all the keys of the left subtree of the node and strictly smaller than all the keys of the right subtree of the node.

Also you are given a set of search keys, all of which are distinct and differ from the node keys contained in the tree. For each key from the set its search in the tree is realised. The search is arranged like this: initially we are located in the tree root, if the key of the current node is larger that our search key, then we move to the left child of the node, otherwise we go to the right child of the node and the process is repeated. As it is guaranteed that the search key is not contained in the tree, the search will always finish in some leaf. The key lying in the leaf is declared the search result.

It is known for sure that during the search we make a mistake in comparing exactly once, that is we go the wrong way, but we won't make any mistakes later. All possible mistakes are equiprobable, that is we should consider all such searches where exactly one mistake occurs. Your task is to find the expectation (the average value) of the search result for every search key, considering that exactly one mistake occurs in the search. That is, for a set of paths containing exactly one mistake in the given key search, you should count the average value of keys containing in the leaves of those paths.

输入格式

The first line contains an odd integer nn ( 3<=n<10^{5} ), which represents the number of tree nodes. Next nn lines contain node descriptions. The (i+1)(i+1) -th line contains two space-separated integers. The first number is the number of parent of the ii -st node and the second number is the key lying in the ii -th node. The next line contains an integer kk ( 1<=k<=1051<=k<=10^{5} ), which represents the number of keys for which you should count the average value of search results containing one mistake. Next kk lines contain the actual keys, one key per line.

All node keys and all search keys are positive integers, not exceeding 10910^{9} . All n+kn+k keys are distinct.

All nodes are numbered from 11 to nn . For the tree root "-1" (without the quote) will be given instead of the parent's node number. It is guaranteed that the correct binary search tree is given. For each node except for the root, it could be determined according to its key whether it is the left child or the right one.

输出格式

Print kk real numbers which are the expectations of answers for the keys specified in the input. The answer should differ from the correct one with the measure of absolute or relative error not exceeding 10910^{-9} .

输入输出样例

  • 输入#1

    7
    -1 8
    1 4
    1 12
    2 2
    2 6
    3 10
    3 14
    1
    1
    

    输出#1

    8.0000000000
    
  • 输入#2

    3
    -1 5
    1 3
    1 7
    6
    1
    2
    4
    6
    8
    9
    

    输出#2

    7.0000000000
    7.0000000000
    7.0000000000
    3.0000000000
    3.0000000000
    3.0000000000
    

说明/提示

In the first sample the search of key 1 with one error results in two paths in the trees: (1, 2, 5) and (1, 3, 6), in parentheses are listed numbers of nodes from the root to a leaf. The keys in the leaves of those paths are equal to 6 and 10 correspondingly, that's why the answer is equal to 8.

首页