luogu#P7952. [✗✓OI R1] 天动万象
[✗✓OI R1] 天动万象
Background

Problem Description
The Geo Archon gives you a rooted tree with root . Each node has a weight . You need to support the following operations:
- : Query the maximum value in the subtree rooted at .
- : For every node in the subtree rooted at , change its weight simultaneously to the sum of the weights of all its children. That is, perform $\forall x \in \operatorname{subtree}(u), a_x\gets \sum_{y\in \operatorname{son}(x)}a_y$. Here, simultaneously means: when updating a node, its children are considered to remain in the state before this operation is applied.
Input Format
The first line contains two positive integers , representing the size of the tree and the number of queries.
The second line contains positive integers, where the -th number is the initial weight of node .
The third line contains integers, where the -th integer represents the parent index of node .
Then follow lines, each containing two integers representing one operation. The specific format is given in the “Description”.
Output Format
For each operation, output one integer per line as the answer.
6 6
1 1 4 5 1 4
1 2 2 4 1
1 2
1 1
2 4
1 2
2 2
1 1
5
5
4
5
15 12
512902574 823918122 595349487 580400545 453562767 72015781 850655655 442513356 619194214 644523811 935104539 371670625 477236621 785497862 282980318
1 2 2 1 2 6 2 2 4 9 6 4 12 1
2 5
2 1
2 1
2 4
2 15
1 2
1 3
2 2
2 7
1 1
1 2
1 6
3279191251
0
2309473383
785497862
0
Hint
[Sample Explanation]
Before any modification, the weights of each node are .
After the first modification, they become .
After the second modification, they become .
[Constraints]
The input size of this problem is large, so please pay attention to constant-factor optimizations.
For of the data, , , , and .
Below are the subtasks (a blank field means there is no special property). You must pass all test points in a subtask and all test points in the subtasks it depends on to get the score for that subtask:
| Subtask ID | Points | Special Properties | Dependencies | |
|---|---|---|---|---|
| 0 | 3 | E | ||
| 1 | 6 | A, B | ||
| 2 | 8 | C | ||
| 3 | 4 | D | ||
| 4 | 13 | E | Subtask0 | |
| 5 | 36 | B | Subtask1 | |
| 6 | 30 | Subtask0~5 |
Special properties:
- For test points with special property A, it is guaranteed that in every operation of type , .
- For test points with special property B, it is guaranteed that in every operation of type , .
- For test points with special property C, it is guaranteed that for all nodes , . In other words, the tree is a chain.
- For test points with special property D, it is guaranteed that for all nodes , . In other words, the tree is a star.
- For test points with special property E, it is guaranteed that the testdata is random. Here, random means that operations and appear with equal probability, is chosen uniformly at random from , and is chosen uniformly at random from .
Hint: Since the problem setter is very lazy and Luogu cannot upload too many test points, the testdata of this problem may be relatively weak. Also, to avoid longer running time due to heavy load on the contest judge machines, the time limit has been relaxed. Therefore, everyone is welcome to try passing this problem with strange hacky approaches.
May we cleanse the four directions, and guard a corner of the mortal world.
Translated by ChatGPT 5