PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000079. Bài đố về cây (Tree Quiz)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bài đố về cây (Tree Quiz)

Tree Quiz

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A rooted tree of nn vertices is specified by parent indices; the root has parent 00. For every ordered pair (x,y)(x,y) with 1≤x,y≤n1\le x,y\le n, compute F(x,y)=(x−1)n2+(LCA⁡(x,y)−1)n+(y−1)F(x,y)=(x-1)n^2+(\operatorname{LCA}(x,y)-1)n+(y-1) All n2n^2 values, including any duplicates, are sorted in non-decreasing order. Each query kk requests the kk-th value, with 1-based indexing. Be careful: the numerical expression may overflow 32-bit integers.

Input

First line: n qn\ q. Next line: nn parent indices p1,…,pnp_1,\ldots,p_n (exactly one is 00 for the root; others specify the parent). Then qq lines each containing an integer kk.

Output

Print the selected sorted sequence value for each query.

Subtasks

General constraints: 1≤n,q≤1051\le n,q\le10^5; 0≤pi≤n0\le p_i\le n; các cha tạo một cây; 1≤k≤n21\le k\le n^2; giá trị cần kiểu số nguyên 64 bit.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

5 3
3 0 2 2 3
1
18
25

Output

0
82
124

Explanation

The minimum occurs for (x,y)=(1,1)(x,y)=(1,1) and equals 00. The maximum occurs for (5,5)(5,5) and equals (5−1)⋅25+(5−1)⋅5+(5−1)=124(5-1)\cdot25+(5-1)\cdot5+(5-1)=124. Rank 1818 has value 8282.