P3401 Luogu Tree
Background
The cute Created_equal little hamster planted a Luogu tree.
(This background was randomly written by the "laji" little hamster, QAQ).
Description
A tree is an undirected, connected graph without cycles, consisting of $n$ vertices and $n-1$ edges. The path between two vertices in a tree is defined as the unique simple path between them; this is obviously a shortest path.
Now we introduce the concept of a subpath. Suppose the path between two vertices $p_1$ and $p_n$ on the tree is $P = \langle p_1,p_2,p_3, \ldots, p_n \rangle$. A subpath is any path $P'$ such that $P'= \langle p_i,p_{i+1},p_{i+2},\ldots,p_j \rangle$, where $1\le i \le j \le n$. Clearly, the original path itself is a subpath, and any single vertex can also be considered a subpath.
We assign a weight to every edge. The adorable Sugar asks the little hamster: for any two vertices $u$ and $v$, can you quickly find the sum of the $\text{xor}$ values of edge weights over all subpaths along the path from $u$ to $v$. Specifically, take all subpaths on the path from $u$ to $v$, for each subpath compute the $\text{xor}$ of the edge weights it traverses, and finally sum up all these $\text{xor}$ values.
What? You do not know $\text{xor}$? Go Baidu it.
At this point, fjzzq2002 showed up: I also want you to support an operation that modifies the weight of an edge.
The little hamster is so "laji" that he certainly cannot solve this problem, so he turns to you for help.
Input Format
The first line contains two positive integers $n$ and $q$, denoting the number of vertices and the total number of operations.
Each of the next $n-1$ lines contains two positive integers $u,v$ and a non-negative integer $w$, indicating there is an edge of weight $w$ between vertices $u$ and $v$.
Each of the next $q$ lines is in the format `1 u v` or `2 u v w`.
If it is operation `1 u v`, you need to output the sum of the $\text{xor}$ values of edge weights over all subpaths on the path from $u$ to $v$.
If it is operation `2 u v w`, you need to change the weight of the edge between $u$ and $v$ to $w$, and it is guaranteed that this edge exists.
Output Format
For each operation of type `1`, output the answer.
Explanation/Hint
|Test point id|$n=$|$q=$|Notes|
|:-:|:-:|:-:|:-:|
|$1$|$100$|$5$|None|
|$2$|$100$|$20$|None|
|$3$|$100$|$100$|None|
|$4$|$5\times 10^3$|$10^3$|None|
|$5$|$5\times 10^3$|$2\times 10^3$|None|
|$6$|$5\times 10^3$|$3\times 10^3$|None|
|$7$|$10^4$|$10^4$|The $i$-th edge connects vertex $i$ and vertex $i+1$, and there is no $2$ operation.|
|$8$|$10^4$|$2\times 10^4$|The $i$-th edge connects vertex $i$ and vertex $i+1$, and there is no $2$ operation.|
|$9$|$10^4$|$10^4$|The $i$-th edge connects vertex $i$ and vertex $i+1$.|
|$10$|$10^4$|$2\times 10^4$|The $i$-th edge connects vertex $i$ and vertex $i+1$.|
|$11$|$10^4$|$10^4$|No $2$ operation.|
|$12$|$10^4$|$2\times 10^4$|No $2$ operation.|
|$13$|$2\times 10^4$|$2\times 10^4$|No $2$ operation.|
|$14$|$3\times 10^4$|$3\times 10^4$|No $2$ operation.|
|$15$|$3\times 10^4$|$10^4$|None|
|$16$|$2\times 10^4$|$2\times 10^4$|None|
|$17$|$2\times 10^4$|$2\times 10^4$|None|
|$18$|$3\times 10^4$|$2\times 10^4$|None|
|$19$|$2\times 10^4$|$3\times 10^4$|None|
|$20$|$3\times 10^4$|$3\times 10^4$|None|
For $100\%$ of the testdata, all edge weights are at most $1023$.
Translated by ChatGPT 5