#P6041. 「ACOI2020」布丁暗杀计划
「ACOI2020」布丁暗杀计划
Description
Finally, the students finished the pudding. Kaede, who loves pudding the most, started thinking: how tasty is this pudding?
How tasty a pudding is depends on the flavor-changing ingredients inside. Different ingredients have different colors and tastiness values.
There are kinds of flavoring ingredients in the pudding, connected by links. The first ingredient is at the top, which is the root of this tree.
Now, Kaede defines that the tastiness of the pudding depends on two given values . This tastiness is defined as follows: let be the -th ancestor of ingredient . Consider all -th level children of ingredient , and among them, take those -th level children whose color is the same as the color of ingredient . The answer is the sum of the tastiness values of these selected nodes. It can be seen that when or changes, in general the tastiness value also changes. Therefore, there are queries to ask you.
In particular, if ingredient does not have a -th ancestor, output directly.
Input Format
The first line contains two integers , meaning there are ingredients and queries.
The second line contains integers. The -th integer represents the color of ingredient , .
The third line contains integers. The -th integer represents the tastiness value of ingredient , .
The fourth line contains integers. The -th integer represents the parent node of the ingredient , .
The next lines each contain two integers , representing a query.
Output Format
There are lines, each containing one integer, representing the answer to the query.
3 1
1 1 1
1 2 3
1 1
2 1
5
5 3
1 2 2 1 1
1 5 2 4 2
1 1 2 3
3 1
4 2
5 1
0
6
0
Hint
Sample Explanation #1

The -st ancestor of ingredient is ingredient . The -st level children of ingredient are ingredients and . The colors of ingredients and are both the same as the color of ingredient , so the sum of tastiness values is .
Constraints
This problem uses bundled testdata.
- Subtask 1 (30 points): Eggs are scarce, the pudding is small: , .
- Subtask 2 (20 points): The ingredients form a chain: , , .
- Subtask 3 (50 points): No special restrictions on the testdata.
For of the testdata, , .
Hint
The time limit for the test points in Subtask 2 and Subtask 3 is 2S.
Translated by ChatGPT 5
京公网安备 11011102002149号