#P6071. 『MdOI R1』Treequery

『MdOI R1』Treequery

Description

Given an undirected tree with nn nodes, where each edge has a weight.

Let E(x,y)E(x,y) denote the set of all edges on the simple path between xx and yy in the tree. In particular, when x=yx=y, E(x,y)=E(x,y)=\varnothing.

You need to answer qq queries online. Each query gives p,l,rp,l,r. Please compute the sum of edge weights of all edges in the set i=lrE(p,i)\bigcap_{i=l}^r E(p,i), i.e. the sum of edge weights contained in the intersection of E(p,lr)E(p, l\dots r).

In simple terms, you need to find the length of the common part of the simple paths from pp to every node in [l,r][l,r].

Input Format

The first line contains two integers n,qn,q, representing the number of nodes in the tree and the number of queries.

The next n1n-1 lines each contain three integers x,y,wx,y,w, indicating that there is an edge between xx and yy with weight ww.

The next qq lines each contain three integers p0,l0,r0p_0,l_0,r_0. For the ii-th query, the actual p,l,rp,l,r are obtained by XOR-ing p0,l0,r0p_0,l_0,r_0 with lastanslastans, respectively, where lastanslastans is the answer to the previous query and is initially 00.

Output Format

For each query, output one integer per line, representing the answer.

5 4
3 1 2
1 5 1
2 3 3
3 4 4
2 3 5
2 1 7
0 7 7
5 5 2
3
2
6
0
10 10
2 1 9907
3 2 8329
4 2 8402
5 4 3636
6 4 8747
7 4 3080
8 6 780
9 6 5414
10 9 3545
2 10 10
26107 26106 26101
4 9 10
14171 14166 14169
8958 8949 8949
36008 36014 36013
11485 11485 11472
3 9 9
30888 30894 30895
8404 8404 8411

26108
0
14161
8959
36015
11482
0
30892
8402
0

Hint

[Sample 1 Explanation]

The tree in the sample is shown in the figure:

In the explanations below, all query parameters are the real values after XOR with lastanslastans.

For the first query, p=2p=2, l=3l=3, r=5r=5, and i=35E(2,i)\bigcap_{i=3}^5 E(2,i) is the edge (2,3)(2,3), with total length 33.

For the second query, p=1p=1, l=2l=2, r=4r=4, and i=24E(1,i)\bigcap_{i=2}^4 E(1,i) is the edge (1,3)(1,3), with total length 22.

For the third query, p=2p=2, l=5l=5, r=5r=5, and i=55E(2,i)\bigcap_{i=5}^5 E(2,i) is the edge set {(2,3),(3,1),(1,5)}\{(2,3),(3,1),(1,5)\}, with total length 66.

For the fourth query, p=3p=3, l=3l=3, r=4r=4, i=34E(3,i)=\bigcap_{i=3}^4 E(3,i)=\varnothing, and the total length is 00.


[Constraints]

This problem uses bundled testdata.

Subtask ID n,qn,q\leq Special Property Score
1 10510^5 l=rl=r 8
2 p=1p=1 20
3 10310^3 None
4 10510^5 26
5 2×1052\times 10^5

For 100%100\% of the data, 1n,q2×1051\leq n,q\leq 2\times 10^5, 1x,y,pn1\leq x,y,p\leq n, 1lrn1\leq l\leq r\leq n, and 1w1041\leq w\leq 10^4.

Translated by ChatGPT 5