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

#BS0000225. Tổ tiên của tôi (My Ancestor)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổ tiên của tôi (My Ancestor)

My Ancestor

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An independently specified Phuoc Hung OJ exercise inspired by the My Ancestor model in Competitive Programming. Vertex numbering, value bounds and query format are defined for this version.

A tree has nn vertices numbered from 11 to nn and is rooted at vertex 11. Each vertex ii has an integer weight aia_i. For each vertex i>1i>1, its parent is pip_i. Weights strictly increase from parent to child: api<aia_{p_i}<a_i.

An ancestor of vertex vv is any vertex on the path from the root to vv, excluding vv itself. There are qq queries. Each query gives a vertex vv and a threshold PP. Find the ancestor uu of vv closest to the root such that au≥Pa_u\ge P. If no such ancestor exists, answer -1.

Input

The first line contains two integers n,qn,q.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

The third line contains n−1n-1 integers p2,p3,…,pnp_2,p_3,\ldots,p_n, the parents of vertices 22 through nn respectively. If n=1n=1, this line is empty.

Each of the following qq lines contains two integers v,Pv,P describing one query.

Output

Print qq lines in query order. Each line contains the index of the qualifying ancestor closest to the root, or -1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤1001\le n,q\le100; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n; 1≤pi<i1\le p_i<i and api<aia_{p_i}<a_i for every 2≤i≤n2\le i\le n; 1≤v≤n1\le v\le n, 1≤P≤1091\le P\le10^9 in every query.
  • Subtask 2 (30 points): 1≤n≤800001\le n\le80000; 1≤q≤200001\le q\le20000; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n; pi=i−1p_i=i-1 and api<aia_{p_i}<a_i for every 2≤i≤n2\le i\le n; 1≤v≤n1\le v\le n, 1≤P≤1091\le P\le10^9 in every query.
  • Subtask 3 (50 points): 1≤n≤800001\le n\le80000; 1≤q≤200001\le q\le20000; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n; 1≤pi<i1\le p_i<i and api<aia_{p_i}<a_i for every 2≤i≤n2\le i\le n; 1≤v≤n1\le v\le n, 1≤P≤1091\le P\le10^9 in every query.

Examples

Input

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

Output

2
-1
1
-1
1

Explanation

The tree has edges 1−2,1−3,2−4,2−51-2,1-3,2-4,2-5.

  • Query (4,4)(4,4): the ancestors of 44 in root-to-vertex order are 1,21,2, with weights 3,53,5. Only vertex 22 qualifies.
  • Query (5,6)(5,6): the ancestors of 55 are 1,21,2, both with weights below 66. Vertex 55 is not its own ancestor.
  • Query (3,2)(3,2): its only ancestor is vertex 11, with weight 3≥23\ge2.
  • Query (1,1)(1,1): the root has no ancestors.
  • Query (4,3)(4,3): both vertices 11 and 22 qualify; vertex 11 is closest to the root.