#P3781. [SDOI2017] 切树游戏
[SDOI2017] 切树游戏
Description
Xiao Q is someone who loves to learn. He often goes to Wikipedia to study computer science.
Just now, Xiao Q carefully studied a series of bitwise operators, among which the bitwise XOR operator impressed him a lot. The bitwise XOR operator is a binary operator. Bitwise XOR is commutative, i.e., .
He found that bitwise XOR can be understood as: for the binary representations of the numbers being operated on, if the corresponding bits are the same, then the result’s bit is , otherwise it is , for example: .
He also found that bitwise XOR can be understood as performing addition without carry on each binary bit of the numbers involved, for example: .
Now Xiao Q has an unrooted tree with nodes, numbered from to , where the weight of node is .
Define the value of a tree as the XOR of the weights of all its nodes. A connected subtree of a tree is a connected subgraph of that is also a tree.
Xiao Q wants to play a cut-tree game on this tree. He will repeatedly perform the following two operations:
- Change x y: change the weight of node to .
- Query k: ask how many nonempty connected subtrees of have value exactly .
Xiao Q really likes (but is not good at) math, and he hopes you can answer his questions quickly. Can you write a program to help him?
Input Format
- The first line contains two positive integers , , representing the number of nodes and the upper bound of weights.
- The second line contains nonnegative integers , representing the initial weight of each node.
- The next lines each contain two positive integers , , indicating that there is an undirected tree edge connecting and .
- The next line contains a positive integer , representing the number of operations Xiao Q performs.
- The next lines each describe one operation in order.
Output Format
Output several lines, each containing one integer, answering each query in order. Since the answer may be large, output it modulo .
4 4
2 0 1 3
1 2
1 3
1 4
12
Query 0
Query 1
Query 2
Query 3
Change 1 0
Change 2 1
Change 3 3
Change 4 1
Query 0
Query 1
Query 2
Query 3
3
3
2
3
2
4
2
3
Hint
Constraints: For of the testdata, , , and the number of Change operations does not exceed .

Translated by ChatGPT 5
京公网安备 11011102002149号