#P3401. 洛谷树

    ID: 2201 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树洛谷原创树链剖分,树剖洛谷月赛

洛谷树

Description

A tree is an undirected, connected graph without cycles, consisting of nn vertices and n1n-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 p1p_1 and pnp_n on the tree is P=p1,p2,p3,,pnP = \langle p_1,p_2,p_3, \ldots, p_n \rangle. A subpath is any path PP' such that P=pi,pi+1,pi+2,,pjP'= \langle p_i,p_{i+1},p_{i+2},\ldots,p_j \rangle, where 1ijn1\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 uu and vv, can you quickly find the sum of the xor\text{xor} values of edge weights over all subpaths along the path from uu to vv. Specifically, take all subpaths on the path from uu to vv, for each subpath compute the xor\text{xor} of the edge weights it traverses, and finally sum up all these xor\text{xor} values.

What? You do not know xor\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 nn and qq, denoting the number of vertices and the total number of operations.

Each of the next n1n-1 lines contains two positive integers u,vu,v and a non-negative integer ww, indicating there is an edge of weight ww between vertices uu and vv.

Each of the next qq 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 xor\text{xor} values of edge weights over all subpaths on the path from uu to vv.
If it is operation 2 u v w, you need to change the weight of the edge between uu and vv to ww, and it is guaranteed that this edge exists.

Output Format

For each operation of type 1, output the answer.

5 3
1 2 3
2 3 3
2 4 6
4 5 1
1 3 4
2 2 4 7
1 3 5
14
26

Hint

Test point id n=n= q=q= Notes
11 100100 55 None
22 2020
33 100100
44 5×1035\times 10^3 10310^3
55 2×1032\times 10^3
66 3×1033\times 10^3
77 10410^4 10410^4 The ii-th edge connects vertex ii and vertex i+1i+1, and there is no 22 operation.
88 2×1042\times 10^4
99 10410^4 The ii-th edge connects vertex ii and vertex i+1i+1.
1010 2×1042\times 10^4
1111 10410^4 No 22 operation.
1212 2×1042\times 10^4
1313 2×1042\times 10^4
1414 3×1043\times 10^4 3×1043\times 10^4
1515 10410^4 None
1616 2×1042\times 10^4 2×1042\times 10^4
1717
1818 3×1043\times 10^4
1919 2×1042\times 10^4 3×1043\times 10^4
2020 3×1043\times 10^4

For 100%100\% of the testdata, all edge weights are at most 10231023.

Translated by ChatGPT 5