#P7788. Tokitsukaze and Colorful Tree
Tokitsukaze and Colorful Tree
Tokitsukaze and Colorful Tree
Problem Description
Tokitsukaze has a rooted tree with nodes, whose nodes are labeled from to and whose root is node . Furthermore, each node has its own color and its own weight , where the color is labeled as a number between and , representing one of given colors. Here are two types of operation: ● -- set as . ● -- set as . Tokitsukaze wants to execute opeations and for , after finishing the first operations, she wants to know the value of $$\sum_{\substack{1 \leq u < v \leq n \ col_u = col_v \ \text{node }u\text{ is not an ancestor of node }v \ \text{node }v\text{ is not an ancestor of node }u}}{(val_u \oplus val_v)}$$ where is the bitwise exclusive OR operation. If you are not familiar with the bitwise exclusive OR, you may refer to http://en.wikipedia.org/wiki/Bitwise_operation#XOR and http://baike.baidu.com/item/xor/64178 .
Input
There are several test cases. The first line contains an integer (), denoting the number of test cases. Then follow all the test cases. For each test case, the first line contains an integer , denoting the number of nodes. The second line contains integers, where the -th intger is , denoting the color of node at the beginning. The third line contains integers, where the -th intger is , denoting the weight of node at the beginning. The next lines describe the tree, where each line contains two integers and , representing an edge connecting node and node . The next line contains an integer , denoting the number of operations. The next lines describe these operations in chronological order of occurrence, where each line contains three integers such that ● if the first integer is , then the following two integers are and , representing an operation of the first type, or ● otherwise the first integer is , and the following two integers are and , representing an operation of the second type. It is guaranteed that the size of the standard input file does not exceed MiB, so you may need to use a fast approach to read the input data.
Output
For each test case, output lines, where the -th line contains an integer, denoting the value Tokitsukaze wants to know after the first operations.
Sample Input
1
5
1 1 1 1 1
1 2 4 8 16
1 2
3 1
2 4
2 5
2
1 3 32
2 3 2
Sample Output
62
146
24
Hint
For the only sample case: ● before the first operation, the value is (2 ⊕ 4) + (4 ⊕ 8) + (4 ⊕ 16) + (8 ⊕ 16) = 6 + 12 + 20 + 24 = 62; ● before the second operation, the value is (2 ⊕ 32) + (32 ⊕ 8) + (32 ⊕ 16) + (8 ⊕ 16) = 34 + 40 + 48 + 24 = 146; ● after all the operations, the value is 8 ⊕ 16 = 24.
Source
2020 Multi-University Training Contest 3