#P16035. [CSPro 33] 文件夹合并

[CSPro 33] 文件夹合并

Background

Luogu’s testdata is only for non-official communication and is not official testdata. Official judging link: https://www.cspro.org/.

Problem Description

Little C, who has just joined Xixi Eiffel Island Co., Ltd., has taken over the project from Little S, who was just promoted. However, when Little C opened the project, the layers of nested folders made Little C feel dizzy. To simplify the project structure, Little C decided to perform some necessary merges on the project folders.

There are nn folders in the project. For convenience, we number these nn folders with integers from 11 to nn, where folder 11 is the root folder of the project. Every other folder has exactly one parent folder, and these folders form a tree structure. Besides subfolders, folder ii also directly stores did_i bytes of data.

Little C performed several folder merge operations. In each operation, Little C chooses a folder xjx_j and merges this folder together with all its subfolders. Specifically, Little C does the following: for each child folder yy of xjx_j, move all folders and files contained in folder yy into folder xjx_j, and then delete folder yy. All file and folder names are pairwise distinct, so there is no need to consider name conflicts during merging. After each merge operation, Little C needs to know how many folders and how many bytes of data are contained in folder xjx_j.

For example, consider the following project: the root folder contains folder 22 and folder 33 and 100100 bytes of data, where folder 22 is empty, and folder 33 contains 200200 bytes of data and folder 44, and folder 44 contains 300300 bytes of data. After one merge on the root folder, folder 22 and folder 33 are merged into the root folder. Now under the root folder there is folder 44 and 300300 bytes of data, and folder 44 also contains 300300 bytes of data.

During the merging process, Little C often needs to access files under some folder zjz_j. At this time, Little C starts from the root folder and each time enters one child folder of the current folder. Little C needs to know, following the above process, what is the minimum number of folders that must be passed through to reach the files under folder zjz_j.

For example, in the above project, before merging the root folder, accessing files under the root folder only requires passing through the root folder, while accessing folder 44 requires passing through the root folder as well as folder 33 and 44. After merging the root folder, accessing folder 44 only requires passing through the root folder and folder 44.

In the whole project, Little C performed a total of mm folder merge and file access operations. You need to help Little C correctly maintain the relationships between folders, and after each operation, correctly answer the required data.

Input Format

Read input from standard input.

The first line contains two integers n,mn, m, representing the number of folders and the number of operations.

The second line contains (n−1)(n - 1) integers f2,⋯ ,fnf_2, \cdots , f_n, where fif_i is the index of the parent folder of folder ii.

The third line contains nn integers d1,d2,⋯ ,dnd_1, d_2, \cdots , d_n, where did_i is the amount of data stored in folder ii.

Then follow mm lines. On line jj, there are two integers. The first integer opjop_j indicates the operation type. If opj=1op_j = 1, it indicates a folder merge operation, followed by an integer xjx_j which is the index of the folder to be merged; if opj=2op_j = 2, it indicates a file access operation, followed by an integer zjz_j which is the index of the folder to be accessed.

Output Format

Write output to standard output.

Output mm lines. Line jj describes the data Little C needs for the jj-th operation: if opj=1op_j = 1, output two integers, in order, the number of subfolders of folder xjx_j and the amount of stored data; if opj=2op_j = 2, output one integer, the minimum number of folders Little C needs to pass through to obtain the data under folder zjz_j.

4 6
1 1 3
100 0 200 300
2 1
2 4
1 1
2 4
1 1
1 1
1
3
1 300
2
0 600
0 600

Hint

Subtasks

For all testdata,

  • 1≤n≤5×105,1≤m≤3×n1 \le n \le 5 \times 10^5, 1 \le m \le 3 \times n,
  • 1≤fi≤n1 \le f_i \le n, the input folder structure forms a tree,
  • 0≤di≤1050 \le d_i \le 10^5,
  • 1≤xj,zj≤n1 \le x_j, z_j \le n, in each merge operation, the given folder xjx_j is not deleted, and in each file access operation, the given folder zjz_j is not deleted.
Subtask ID n≤n \le Special Property Score
1 500 None 10
2 5,000 ^ 15
3 10510^5 ^
4 5×1055 \times 10^5 A 5
5 ^ B ^
6 C 10
7 D 15
8 E 10
9 None 15

Special Property A: fi=(i−1)f_i = (i - 1).

Special Property B: fi=1f_i = 1.

Special Property C: in folder merge operations, xj=1x_j = 1.

Special Property D: opj=1op_j = 1, i.e. there are no file access operations.

Special Property E: opj=2op_j = 2, i.e. there are no folder merge operations.

Translated by ChatGPT 5