#P7826. 「RdOI R3」RBT
「RdOI R3」RBT
Description
You have a rooted tree with root , with nodes numbered . Node has an integer weight . At the same time, every node is colored red or blue. Initially, all nodes are red. You need to maintain this tree and process operations. There are several types of operations, with the following formats:
1 x v: Increase the weight of every node in the subtree of node by , and take modulo .2 x v: Set to . Note that this does not assign the entire subtree.3 x: Color node blue. Let node be the predecessor of node in the ranking by node index (not by weight) among its red sibling nodes. Delete the edge connecting node to its parent. Then attach node as a child of node . If node has no red siblings, or all its red siblings have indices greater than , then do nothing.4 x: Let be the set of weights in the subtree of whose occurrence counts are odd. Output the sum of the -th power of each number in the set, modulo . That is, .
In particular, the testdata guarantees that each node can be operated by operation 3 at most once. In other words, there will be no case where operation 3 is applied to a blue node.
Input Format
The first line contains four integers .
The second line contains integers, representing the initial .
The next lines each contain two integers , representing an undirected tree edge .
The next lines each contain two or three integers .
Output Format
For each query operation, output one integer per line as the answer.
10 10 500 1
3 2 1 2 1 3 2 1 3 4
1 4
7 8
3 6
8 10
2 3
2 5
8 9
7 2
2 1
4 1
1 3 1
2 1 2
4 1
4 3
4 6
1 4 1
3 7
1 5 1
4 5
10
5
6
4
12
Hint
Sample Explanation
Sample #1
- Query
4 1: The weights in the subtree are . The set is , so the answer is . - Update
1 3 1: The weights of all nodes become . - Update
2 1 2: The weights of all nodes become . - Query
4 1: The weights in the subtree are . The set is , so the answer is . - Query
4 3: The nodes in the subtree are , with weights . The set is , so the answer is . - Query
4 6: Since this is a leaf node, the subtree contains only itself with weight . The set is , so the answer is . - Update
1 4 1: The weights of all nodes become . - Update
3 7: Color blue, delete the edge , and attach as a child of . - Update
1 5 1: The weights of all nodes become . - Query
4 5: The nodes in the subtree are , with weights . The set is , so the answer is .
Constraints
This problem uses bundled testdata.
For all testdata, , , , , .
| subtask | score | special constraints |
|---|---|---|
| and the initial tree shape are generated uniformly at random | ||
| none |
Translated by ChatGPT 5
京公网安备 11011102002149号