Logo
(追記) (追記ここまで)

31603번 - Tree Quiz 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 1024 MB3301129139.394%

문제

Your friend wants to quiz you. You are given a rooted tree with $n$ nodes, numbered from 1ドル$ to $n$. For every node $i,ドル its parent is node $p_i,ドル except for the root (the node without a parent) which has $p_i = 0$. Node $u$ is an ancestor of node $v$ if either $u = v,ドル or node $u$ is an ancestor of the parent of node $v$ (if it exists).

We say that node $z$ is a common ancestor of nodes $x$ and $y$ if node $z$ is an ancestor of both nodes $x$ and $y$. We say that node $z$ is the lowest common ancestor of nodes $x$ and $y$ if it is a common ancestor of nodes $x$ and $y,ドル and every common ancestor of nodes $x$ and $y$ is also an ancestor of node $z$. We denote the lowest common ancestor of nodes $x$ and $y$ by $LCA(x, y)$. In particular, $LCA(x, x) = x$.

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 $q$ questions, numbered from 1ドル$ to $q$. In question $j,ドル you are given an integer $k_j$ and asked to find the $k_j$-th element of the array $L$. Note that $L$ is 1ドル$-indexed, so the indices range from 1ドル$ to $n^2,ドル inclusive. To pass the quiz, you have to answer all of the questions.

입력

The first line of input contains two integers $n$ and $q$ (1ドル ≤ n ≤ 100,円 000$; 1ドル ≤ q ≤ 100,円 000$). The second line contains $n$ integers $p_1, p_2, \dots , p_n$ (0ドル ≤ p_i ≤ n$ for all $i$). It is guaranteed that the given values represent a rooted tree. Each of the next $q$ lines contains an integer. The $j$-th line contains $k_j$ (1ドル ≤ k_j ≤ n^2$).

출력

For each question in order, output an integer representing the answer to the question.

제한

예제 입력 1

5 3
3 0 2 2 3
1
18
25

예제 출력 1

0
82
124

The tree in the input is illustrated by Figure K.1.

Figure K.1: Illustration of the tree in sample input #1.

The elements of $L$ are $(0, 6, 8, 12, 14, 30, 31, 32, 33, 34, 56, 58, 60, 62, 64, 80, 81, 82, 84, 93, 106, 108, 110, 112, 124)$.

힌트

출처

ICPC > Regionals > Asia Pacific > Asia Pacific Championship > The 2024 ICPC Asia Pacific Championship K번

ICPC > Regionals > Asia Pacific > Asia Pacific Championship > The 2025 ICPC Asia Pacific Championship 연습 세션 D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /