#P3781. [SDOI2017] 切树游戏

    ID: 2724 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017线段树各省省选山东O2优化树链剖分,树剖矩阵乘法

[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 \oplus impressed him a lot. The bitwise XOR operator is a binary operator. Bitwise XOR is commutative, i.e., ij=jii \oplus j = j \oplus i.

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 00, otherwise it is 11, for example: 1(01)2(10)=3(11)1(01) \oplus 2(10) = 3(11).

He also found that bitwise XOR can be understood as performing addition without carry on each binary bit of the numbers involved, for example: 3(11)3(11)=0(00)3(11) \oplus 3(11) = 0(00).

Now Xiao Q has an unrooted tree TT with nn nodes, numbered from 11 to nn, where the weight of node ii is viv_i.

Define the value of a tree as the XOR of the weights of all its nodes. A connected subtree of a tree TT is a connected subgraph of TT 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 xx to yy.
  • Query k: ask how many nonempty connected subtrees of TT have value exactly kk.

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 nn, mm, representing the number of nodes and the upper bound of weights.
  • The second line contains nn nonnegative integers v1,v2,,vnv_1, v_2, \dots, v_n, representing the initial weight of each node.
  • The next n1n - 1 lines each contain two positive integers aia_i, bib_i, indicating that there is an undirected tree edge connecting aia_i and bib_i.
  • The next line contains a positive integer qq, representing the number of operations Xiao Q performs.
  • The next qq 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 1000710007.

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 100%100\% of the testdata, 1ai,bi,xn1 \leq a_i, b_i, x \leq n, 0vi,y,k<m0 \leq v_i, y, k < m, and the number of Change operations does not exceed 1000010000.

Translated by ChatGPT 5