#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 folders in the project. For convenience, we number these folders with integers from to , where folder 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 also directly stores bytes of data.
Little C performed several folder merge operations. In each operation, Little C chooses a folder and merges this folder together with all its subfolders. Specifically, Little C does the following: for each child folder of , move all folders and files contained in folder into folder , and then delete folder . 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 .
For example, consider the following project: the root folder contains folder and folder and bytes of data, where folder is empty, and folder contains bytes of data and folder , and folder contains bytes of data. After one merge on the root folder, folder and folder are merged into the root folder. Now under the root folder there is folder and bytes of data, and folder also contains bytes of data.
During the merging process, Little C often needs to access files under some folder . 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 .
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 requires passing through the root folder as well as folder and . After merging the root folder, accessing folder only requires passing through the root folder and folder .
In the whole project, Little C performed a total of 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 , representing the number of folders and the number of operations.
The second line contains integers , where is the index of the parent folder of folder .
The third line contains integers , where is the amount of data stored in folder .
Then follow lines. On line , there are two integers. The first integer indicates the operation type. If , it indicates a folder merge operation, followed by an integer which is the index of the folder to be merged; if , it indicates a file access operation, followed by an integer which is the index of the folder to be accessed.
Output Format
Write output to standard output.
Output lines. Line describes the data Little C needs for the -th operation: if , output two integers, in order, the number of subfolders of folder and the amount of stored data; if , output one integer, the minimum number of folders Little C needs to pass through to obtain the data under folder .
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,
- ,
- , the input folder structure forms a tree,
- ,
- , in each merge operation, the given folder is not deleted, and in each file access operation, the given folder is not deleted.
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| 1 | 500 | None | 10 |
| 2 | 5,000 | ^ | 15 |
| 3 | ^ | ||
| 4 | A | 5 | |
| 5 | ^ | B | ^ |
| 6 | C | 10 | |
| 7 | D | 15 | |
| 8 | E | 10 | |
| 9 | None | 15 |
Special Property A: .
Special Property B: .
Special Property C: in folder merge operations, .
Special Property D: , i.e. there are no file access operations.
Special Property E: , i.e. there are no folder merge operations.
Translated by ChatGPT 5