← Home
write a go solution for Description:
Once Grisha found a tree (connected graph without cycles) with a root in node 1.

But this tree was not just a tree. A permutation p of integers from 0 to n-1 is written in nodes, a number p_i is written in node i.

As Grisha likes to invent some strange and interesting problems for himself, but not always can solve them, you need to help him deal with two types of queries on this tree.

Let's define a function MEX(S), where S is a set of non-negative integers, as a smallest non-negative integer that is not included in this set.

Let l be a simple path in this tree. So let's define indices of nodes which lie on l as u_1, u_2, ldots, u_k.

Define V(l) as a set {p_u_1, p_u_2, ldots , p_u_k}.

Then queries are:

1. For two nodes i and j, swap p_i and p_j.
2. Find the maximum value of MEX(V(l)) in all possible l.

Input Format:
The first line contains a single integer n (2<=n<=2*10^5) — the number of nodes of a tree.

The second line contains n integers — p_1, p_2, ldots, p_n (0<=p_i<n) — the permutation p, it's guaranteed that all numbers are different .

The third line contains n-1 integers — d_2, d_3, ldots, d_n (1<=d_i<i), where d_i is a direct ancestor of node i in a tree.

The fourth line contains a single integer q (1<=q<=2*10^5) — the number of queries.

The following q lines contain the description of queries:

At the beginning of each of next q lines, there is a single integer t (1 or 2) — the type of a query:

1. If t=1, the line also contains two integers i and j (1<=i,j<=n) — the indices of nodes, where values of the permutation should be swapped.
2. If t=2, you need to find the maximum value of MEX(V(l)) in all possible l.

Output Format:
For each type 2 query print a single integer — the answer for this query.

Note:
Number written in brackets is a permutation value of a node.. Output only the code with no comments, explanation, or additional text.