#P7880. [Ynoi2006] rldcot
[Ynoi2006] rldcot
Problem Description
Given a tree with nodes, the root is . Each node has an index, and each edge has a weight.
Define as the sum of edge weights on the simple path from node to the root, and as the lowest common ancestor of nodes and in the tree.
There are queries. In each query, are given. For all ordered pairs of node indices satisfying , ask how many distinct values of there are.
Input Format
The first line contains two integers separated by spaces.
The next lines each contain three integers separated by spaces, meaning there is an edge from to with weight .
The next lines each contain two integers separated by spaces, representing one query.
Output Format
Output lines. Each line contains one integer, the answer to the corresponding query.
10 19
9 1 -4
9 8 2
8 10 5
9 7 -3
1 4 2
10 2 5
10 5 -1
7 3 -3
10 6 5
8 10
4 6
1 7
7 9
5 5
7 8
8 10
10 10
10 10
9 10
5 7
8 8
6 6
2 8
9 10
4 8
5 5
1 6
1 2
3
4
7
3
1
3
3
1
1
2
5
1
1
8
2
7
1
6
2
10 19
6 1 299830931
1 4 -565297395
1 7 -606073228
4 8 94253706
8 9 -576603423
4 3 116780102
3 5 620388954
7 10 -950023905
5 2 813045783
3 5
7 7
8 10
4 7
10 10
9 9
4 7
8 10
6 7
4 8
9 10
2 9
8 10
2 8
4 4
10 10
4 9
1 5
8 9
3
1
4
5
1
1
5
4
3
6
3
9
4
8
1
1
7
5
2
Hint
Idea: nzhtl1477, Solution: nzhtl1477, Code: lk, Data: nzhtl1477.
Constraints:
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For of the testdata, , , all values are integers, and .
Translated by ChatGPT 5