#P5346. 【XR-1】柯南家族
【XR-1】柯南家族
Background
xht37 has recently been addicted to Detective Conan.
In one episode, Ran suspects Conan’s true identity again. To stop Ran from suspecting him, Conan makes up a family background to answer Ran’s questions.
Problem Description
This family initially has only one person. Later, people keep having children, and up to now the family has people. Person is Conan. It is easy to see that this family forms a tree structure with nodes.
To make his made-up family background more realistic, Conan assigns each person an IQ value. However, a person’s smartness is not only related to their IQ value, but may also depend on their ancestors’ smartness and the era when they were born.
Specifically, in this family, A is smarter than B if and only if A and B satisfy one of the following three conditions:
- A’s IQ value is higher than B’s IQ value.
- A’s IQ value equals B’s IQ value, and A and B have different fathers; A’s father is smarter than B’s father.
- A’s IQ value equals B’s IQ value, and A and B have the same father or one of them has no father; A is born later than B.
An obvious conclusion is that no two people in this family are equally smart.
Conan needs to answer Ran’s queries. For convenience, assume the -th person to be born is numbered .
Each query is one of the following three types:
1 x: Ask what rank (in terms of smartness) the person numbered has in the whole family.2 x k: Ask for the number of the -th smartest person among person and all their ancestors.3 x k: Ask for the number of the -th smartest person among person and all their descendants.
Conan still has many cases to solve and does not want to waste time answering Ran’s questions. He hopes you can write a program to help him answer all queries.
Input Format
The first line contains two integers , representing the number of people and the number of queries.
The second line contains integers , where denotes the father of .
The third line contains integers , where denotes the IQ value of .
The next lines each contain two or three integers describing a valid query. The first integer is the query type, and the following one or two integers are the query parameters.
Output Format
Output lines, each containing one integer representing the answer to the query.
5 11
1 1 3 2
1 2 2 1 1
1 1
1 2
1 3
1 4
1 5
2 4 1
2 5 3
3 1 1
3 1 2
3 1 3
3 1 4
5
2
1
3
4
3
1
3
2
4
5
Hint
[Sample Explanation]
The formed tree is as follows:

First compare persons and . Since person has the same IQ value as person and they have the same father, and person is born later than person , condition 3 is satisfied, so person is smarter than person .
Then compare persons and . Since person has the same IQ value as person and they have different fathers, and person ’s father (person ) is smarter than person ’s father (person ), condition 2 is satisfied, so person is smarter than person .
Then compare persons and . Since person has the same IQ value as person and person has no father, and person is born later than person , condition 3 is satisfied, so person is smarter than person .
Then, using condition 1 to compare persons and , we can sort the smartness of the people as: .
[Constraints]
There are test points in total.
For the first of the data, . Each test point is worth points, time limit 1 s.
For another of the data, each person has at most one son. Each test point is worth points, time limit 4 s.
For another of the data, . Each test point is worth points, time limit 1.5 s.
For another of the data, only the first type of query is guaranteed. Each test point is worth points, time limit 1.5 s.
For of the data, , . Each test point is worth points, time limit 2.5 s.
Translated by ChatGPT 5