#P3787. Frozen Watermelons
Frozen Watermelons
Frozen Watermelons
In this problem, you are given a rooted tree with (n) watermelons (nodes) connected by (n-1) magical vines (edges). The tree is rooted at node 1. Each vine has a weight (w_i) such that when a cold energy of value (x) passes through it, the energy is transformed to (x \times w_i). Initially, every watermelon has a coldness value of 0.
There are two types of operations:
-
Update Operation: Given a node (i) and a value (x), emit a beam of cold energy of value (x) at watermelon (i). This cold energy flows only to the subtree of (i) (i.e. from (i) to its descendants). When the cold energy passes through a vine with weight (w_i), it gets multiplied by (w_i). For every node reached along the path (including the starting node), add the (possibly transformed) energy value to its current coldness. In other words, if a node (j) is in the subtree of (i) and the unique path from (i) to (j) goes through vines with weights (w_1, w_2, \dots, w_k), then the coldness at node (j) increases by (x \times (w_1 \times w_2 \times \cdots \times w_k)). Note that when a node has multiple children, the cold energy is copied along each branch independently.
-
Query Operation: Given a node (i), print its current coldness value.
Your task is to process (q) operations on the watermelon tree.
inputFormat
The first line contains two integers (n) and (q) ((1 \leq n, q \leq 10^5)), the number of watermelons and the number of operations.
Then (n-1) lines follow. Each of these lines contains three values (u), (v) and (w) representing an edge from watermelon (u) to watermelon (v) with weight (w). It is guaranteed that these edges form a rooted tree with root 1.
Then (q) lines follow. Each line begins with an integer representing the operation type:
- If the operation is of type 1, the line contains:
1 i x
, meaning an update operation at node (i) with initial cold energy (x). - If the operation is of type 2, the line contains:
2 i
, meaning a query for the current coldness value at node (i).
outputFormat
For each query operation (type 2), output a single line containing the current coldness value of the specified watermelon.
sample
5 7
1 2 2
1 3 3
2 4 2
2 5 1
1 1 10
2 4
2 5
1 2 5
2 4
2 3
2 1
40
20
50
30
10
</p>