Tree Quiz
시간 제한4초메모리 제한1024 MB
모든 순서쌍 (x, y)를 (x, LCA(x, y), y)로 부호화해 정렬한 배열에서 k번째 값을 묻는 질의에 답한다.
문제
Your friend wants to quiz you. You are given a rooted tree with nodes, numbered from to . For every node , its parent is node , except for the root (the node without a parent) which has . Node is an ancestor of node if either , or node is an ancestor of the parent of node (if it exists).
We say that node is a common ancestor of nodes and if node is an ancestor of both nodes and . We say that node is the lowest common ancestor of nodes and if it is a common ancestor of nodes and , and every common ancestor of nodes and is also an ancestor of node . We denote the lowest common ancestor of nodes and by . In particular, .
Your friend would like to run the following pseudocode:
let L be an empty array
for x = 1 to n
for y = 1 to n
append ((x - 1) * n * n + (LCA(x, y) - 1) * n + (y - 1)) to L
sort L in non-decreasing order
Your friend has questions, numbered from to . In question , you are given an integer and asked to find the -th element of the array . Note that is -indexed, so the indices range from to , inclusive. To pass the quiz, you have to answer all of the questions.
입력
The first line of input contains two integers and (; ). The second line contains integers ( for all ). It is guaranteed that the given values represent a rooted tree. Each of the next lines contains an integer. The -th line contains ().
출력
For each question in order, output an integer representing the answer to the question.