#P5346. 【XR-1】柯南家族

    ID: 6065 远端评测题 1000~4000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>倍增离散化O2优化可持久化线段树可持久化后缀数组 SA

【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 nn people. Person nn is Conan. It is easy to see that this family forms a tree structure with nn 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:

  1. A’s IQ value is higher than B’s IQ value.
  2. 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.
  3. 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 qq queries. For convenience, assume the ii-th person to be born is numbered ii.

Each query is one of the following three types:

  1. 1 x: Ask what rank (in terms of smartness) the person numbered xx has in the whole family.
  2. 2 x k: Ask for the number of the kk-th smartest person among person xx and all their ancestors.
  3. 3 x k: Ask for the number of the kk-th smartest person among person xx 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 n,qn, q, representing the number of people and the number of queries.

The second line contains n1n - 1 integers f2nf_{2 \dots n}, where fif_i denotes the father of ii.

The third line contains nn integers a1na_{1 \dots n}, where aia_i denotes the IQ value of ii.

The next qq 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 qq 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 22 and 33. Since person 33 has the same IQ value as person 22 and they have the same father, and person 33 is born later than person 22, condition 3 is satisfied, so person 33 is smarter than person 22.

Then compare persons 44 and 55. Since person 44 has the same IQ value as person 55 and they have different fathers, and person 44’s father (person 33) is smarter than person 55’s father (person 22), condition 2 is satisfied, so person 44 is smarter than person 55.

Then compare persons 11 and 55. Since person 55 has the same IQ value as person 11 and person 11 has no father, and person 55 is born later than person 11, condition 3 is satisfied, so person 55 is smarter than person 11.

Then, using condition 1 to compare persons 22 and 44, we can sort the smartness of the 55 people as: 3>2>4>5>13 > 2 > 4 > 5 > 1.

[Constraints]

There are 1010 test points in total.

For the first 20%20\% of the data, 1n,q1031 \le n, q \le 10 ^ 3. Each test point is worth 77 points, time limit 1 s.

For another 20%20\% of the data, each person has at most one son. Each test point is worth 99 points, time limit 4 s.

For another 20%20\% of the data, 1n,q1051 \le n, q \le 10 ^ 5. Each test point is worth 99 points, time limit 1.5 s.

For another 20%20\% of the data, only the first type of query is guaranteed. Each test point is worth 1212 points, time limit 1.5 s.

For 100%100\% of the data, 1n,q5×1051 \le n, q \le 5 \times 10 ^ 5, 1ai1091 \le a_i \le 10 ^ 9. Each test point is worth 1313 points, time limit 2.5 s.

Translated by ChatGPT 5