Yasya and the Mysterious Tree
Contest: Codeforces Round 950 (Div. 3)
Difficulty rating: 2300
Tags: bitmasks, data structures, dfs and similar, graphs, greedy, strings, trees

Yasya was walking in the forest and accidentally found a tree with $$$n$$$ vertices. A tree is a connected undirected graph with no cycles.

Next to the tree, the girl found an ancient manuscript with $$$m$$$ queries written on it. The queries can be of two types.

The first type of query is described by the integer $$$y$$$. The weight of each edge in the tree is replaced by the bitwise exclusive OR of the weight of that edge and the integer $$$y$$$.

The second type is described by the vertex $$$v$$$ and the integer $$$x$$$. Yasya chooses a vertex $$$u$$$ ($$$1 \le u \le n$$$, $$$u \neq v$$$) and mentally draws a bidirectional edge of weight $$$x$$$ from $$$v$$$ to $$$u$$$ in the tree.

Then Yasya finds a simple cycle in the resulting graph and calculates the bitwise exclusive OR of all the edges in it. She wants to choose a vertex $$$u$$$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $$$u$$$). If an edge between $$$v$$$ and $$$u$$$ already existed, a simple cycle is the path $$$v \to u \to v$$$.

Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.

Help Yasya answer all the queries.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 2 \cdot 10^5$$$) — the number of vertices in the tree and the number of queries.

The next $$$n - 1$$$ lines of each test case contain three integers $$$v$$$, $$$u$$$, $$$w$$$ ($$$1 \le v, u \le n$$$, $$$1 \le w \le 10^9$$$) — the ends of some edge in the tree and its weight.

It is guaranteed that the given set of edges forms a tree.

The next $$$m$$$ lines of each test case describe the queries:

- ^ $$$y$$$ ($$$1 \le y \le 10^9$$$) — parameter of the first type query;
- ? $$$v$$$ $$$x$$$ ($$$1 \le v \le n$$$, $$$1 \le x \le 10^9$$$) — parameters of the second type query.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. The same is guaranteed for $$$m$$$.

Output format:
For each test case, output the answers to the queries of the second type.

Constraints:
Time limit: 2.5 s
Memory limit: 512.0 MB

Example input:
2
3 7
1 2 1
3 1 8
^ 5
? 2 9
^ 1
? 1 10
^ 6
? 3 1
? 2 9
5 6
1 2 777
3 2 2812
4 1 16
5 3 1000000000
^ 4
? 3 123
? 5 1000000000
^ 1000000000
? 1 908070
? 2 1

Example output:
13 15 11 10 
1000000127 2812 999756331 999999756

Example input:
3
8 4
8 6 3
6 3 4
2 5 4
7 6 2
7 1 10
4 1 4
5 1 2
^ 4
^ 7
? 7 8
? 4 10
5 6
3 1 4
2 3 9
4 3 6
5 2 10
? 5 7
^ 1
^ 8
? 4 10
? 1 9
? 3 6
4 2
2 1 4
4 3 5
2 3 4
^ 13
? 1 10

Example output:
14 13 
13 8 11 11 
10

Editorial:
We will hang the tree on the vertex $$$1$$$ and count for each vertex $$$d_v$$$ — xor on the path from it to the root. This can be done by depth-first traversal in $$$O(n)$$$.
Now let's learn how to solve the problem in $$$O(n)$$$ for each query. The first type of query can be executed straightforwardly. Notice that due to the properties of the xor operation, the values will only change for vertices at odd depth (the depth of the root is $$$0$$$). At the same time, they will change trivially: they will be xored with $$$y$$$. To answer the second query, it is necessary to realize that the xor on a simple cycle $$$v \to \text{lca}(v, u) \to u \to v$$$ is equal to $$$d_v \oplus d_u \oplus x$$$. Indeed, the path from $$$\text{lca(v, u)}$$$ to the root will be counted twice, so it will turn into $$$0$$$, and no other extra edges will be included in this xor. With due skill, you can try to speed up such a solution with AVX instructions, but the time constraints were chosen strictly.
For a complete solution to the problem, you can use the data structure prefix tree (trie). With its help, you can find in $$$O(\log x)$$$ for the number $$$x$$$ such a $$$y$$$ in the set, that $$$x \oplus y$$$ is maximal. Since $$$d_v$$$ change differently, you will have to use two tries — for vertices at even and odd heights. Operations of the first type can be accumulated in the variable $$$w_\text{odd}$$$ and added to the xor expression. In addition, you must not forget to remove $$$d_v$$$ from the necessary trie when answering the second query, and then insert it back. To do this, you can maintain a counter of terminal leaves in each vertex and use this information during descent.
Thus, the final asymptotic is $$$O((n + m) \log 10^9)$$$.