#P11330. Dynamic Grape Vine Operations
Dynamic Grape Vine Operations
Dynamic Grape Vine Operations
Syrup owns a huge grapevine with leaves numbered from to . The leaves are connected by branches where the th branch connects leaf and leaf with length . Thus the leaves and branches form a tree.
Initially, there are no grapes on any leaf. Syrup can perform operations of the following three types:
- Water Operation: Given a leaf $j$, water it. If there is no grape on leaf $j$, a grape immediately grows; if there is already a grape present, the grape is removed.
- Update Operation: Given a branch index $k$ (corresponding to the $k$th branch in the input), change its length to a new value $w$.
- Query Operation: When Syrup stands on a leaf $j$, he wants to quickly find the minimum distance from $j$ to any leaf that currently has a grape. The distance between two leaves is defined as the sum of the lengths of the branches along the unique path connecting them. If no grape exists on any leaf, output $-1$.
You are given the tree configuration and the $Q$ operations. Process the operations in order and output the answer for each query operation.
inputFormat
The first line contains two integers and .
The next lines each contain three integers , , and , describing a branch connecting leaves and with length .
Each of the following lines represents an operation in one of the following formats:
- For a water operation: `1 j` (toggle the grape on leaf $j$).
- For an update operation: `2 k w` (update the $k$th branch’s length to $w$; the $k$th branch is the one given in the input order).
- For a query operation: `3 j` (query from leaf $j$ to find the nearest grape).
outputFormat
For each query operation (operation of type 3), output a single line containing an integer which is the minimum distance from the given leaf to any leaf that currently has a grape. If no grape exists, output -1.
sample
4 6
1 2 3
2 3 4
3 4 5
3 1
1 3
3 1
2 2 1
3 3
3 4
-1
7
0
5
</p>