#P3401. 洛谷树
洛谷树
Description
A tree is an undirected, connected graph without cycles, consisting of vertices and 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 and on the tree is . A subpath is any path such that , where . 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 and , can you quickly find the sum of the values of edge weights over all subpaths along the path from to . Specifically, take all subpaths on the path from to , for each subpath compute the of the edge weights it traverses, and finally sum up all these values.
What? You do not know ? 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 and , denoting the number of vertices and the total number of operations.
Each of the next lines contains two positive integers and a non-negative integer , indicating there is an edge of weight between vertices and .
Each of the next 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 values of edge weights over all subpaths on the path from to .
If it is operation 2 u v w, you need to change the weight of the edge between and to , 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 | Notes | ||
|---|---|---|---|
| None | |||
| The -th edge connects vertex and vertex , and there is no operation. | |||
| The -th edge connects vertex and vertex . | |||
| No operation. | |||
| None | |||
For of the testdata, all edge weights are at most .
Translated by ChatGPT 5
京公网安备 11011102002149号