#P3345. [ZJOI2015] 幻想乡战略游戏

[ZJOI2015] 幻想乡战略游戏

Description

The tsundere girl Yuuka is playing a very interesting strategy game. At first, the game map was not too large, and Yuuka could still manage it. But for some reason, the online game developers are making the map larger and larger, to the point that Yuuka can no longer take it all in at a glance, let alone fight others.

Before going to war, Yuuka needs to solve a very basic management problem.

The entire map is a tree with nn empty fields (nodes), connected by n1n - 1 weighted edges, so that there is a unique path between any two nodes.

In the game, Yuuka may increase or decrease troops on empty fields. At the same time, she can place one supply station on a node. If the supply station is at node uu, and there are dvd_v units of troops at node vv, then Yuuka must spend dv×dist(u,v)d_v \times \text{dist}(u,v) money per day to supply these troops. Since Yuuka needs to supply all troops, the total cost is (dv×dist(u,v))\sum (d_v \times \text{dist}(u,v)) (where 1vn1 \le v \le n). Here, dist(u,v)\text{dist}(u,v) denotes the distance between uu and vv on the tree (the sum of weights along the unique path).

Due to the game rules, Yuuka can choose only one node as the supply station. During the game, she may create troops on some nodes or reduce troops on some nodes. After such operations, for economic reasons, she may move the supply station to save money. However, since the map is just too large, Yuuka cannot easily make the optimal arrangement. Can you help her?

You may assume that initially all nodes have no troops.

Input Format

The first line contains two integers nn and QQ, denoting the number of nodes in the tree and the number of Yuuka’s operations, respectively. Nodes are labeled from 11 to nn.

The next n1n - 1 lines each contain three positive integers a,b,ca, b, c, indicating that there is an edge between aa and bb with weight cc.

The next QQ lines each contain two integers u,eu, e, meaning Yuuka adds ee units of troops at node uu (if e<0e < 0, that means Yuuka removes e|e| units at uu; in other words, dudu+ed_u \leftarrow d_u + e).

It is guaranteed that at any time, the number of troops at each node is non-negative.

Output Format

For each of Yuuka’s operations, output the minimal daily cost after the operation, i.e., the cost if Yuuka chooses the optimal supply station.

10 5
1 2 1
2 3 1
2 4 1
1 5 1
2 6 1
2 7 1
5 8 1
7 9 1
1 10 1
3 1
2 1
8 1
3 1
4 1
0
1
4
5
6

Hint

For all testdata, 1c1031 \le c \le 10^3, 0e1030 \le |e| \le 10^3, 1n1051 \le n \le 10^5, 1Q1051 \le Q \le 10^5.

Interestingly, for all testdata, the degree of each node in the tree does not exceed 2020.

Translated by ChatGPT 5