시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 1024 MB318723.333%

문제

Syrup the Turtle waters the huge Grapevine which snakes around town. The layout of the Grapevine can be best described as having $N$ leafy joints, which Syrup has numbered $1$ to $N$, connected by $N - 1$ branches also numbered $1$ to $N - 1$. Each branch $i$ directly connects two joints $A_i$ and $B_i$, and has a length of $W_i$. No two branches directly connect the same pair of joints, and as part of the single Grapevine, every joint is connected to every other either directly or indirectly by branches.

With his sturdy hose and a little handiwork, Syrup is able to sway the growth of the Grapevine as he deems fit. In particular, he can soak a joint of the vine - causing it to immediately sprout a single giant grape if it was empty, or instead dislodging the grape there if one was present.

He can also anneal a branch with water, extending or shortening it to a new length $w_i$ by spraying at the right pressure and angle. To make sure things are on track, Syrup will periodically stand atop an elevated joint and seek for the closest grape. The distance from such a joint to a grape is defined by the shortest path starting from said joint, traversing a number of branches (or none at all), and ending at the grape’s own joint.

Right now, the Grapevine has no grapes attached after the last passing storm. Syrup has his watering agenda of $Q$ actions planned out for the week and is about to begin spraying; but first, he needs to know what distances to expect when he seeks for grapes along the way. Given Syrup’s watering plans, your task is to find for each planned seek the distance from the specified joint to the nearest grape.

입력

Your program must read from standard input.

The first line contains two integers, $N$ and $Q$.

$N - 1$ lines will follow. The $i$th line contains three integers, $A_i$, $B_i$, and $W_i$, describing a single branch.

$Q$ lines will follow, each representing an action by Syrup.

  • If the first integer of the line is $1$, it represents a seek and $1$ integer $q_i$ will follow. This means that you are to determine the distance between joint $q_i$ and the nearest joint with a grape, and output this distance. If there are no grapes on the Grapevine, output $-1$ instead.
  • If the first integer of the line is $2$, it represents a soak and $1$ integer $u_i$ will follow. This means that joint $u_i$ is soaked and grows a grape or has its grape dislodged.
  • If the first integer of the line is $3$, it represents an anneal and $3$ integers $a_i$, $b_i$, and $w_i$ will follow. This means that the length of the branch between joints $a_i$ and $b_i$ has had its length changed to $w_i$. It is guaranteed that a branch between joints $a_i$ and $b_i$ exists.

출력

Your program must print to standard output.

For each seek action, output one line with a single integer, the distance to the closest grape, or $-1$ if no grapes are present.

제한

  • $2 ≤ N ≤ 100\,000$
  • $1 ≤ Q ≤ 100\,000$
  • $1 ≤ A_i \ne B_i ≤ N$
  • $0 ≤ W_i ≤ 10^9$

서브태스크

번호배점제한
16

$N, Q ≤ 2000$

214

For all seek actions, $q_i = 1$

315

The vine forms a complete binary tree, $A_i = ⌊ \frac{i+1}{2} ⌋$, $B_i = i + 1$

415

There is at most $1$ grape on the vine at any point in time.

518

All soak actions will occur before any seek or anneal actions. For all anneal actions, $w_i = 0$

632

-

예제 입력 1

7 11
1 2 2
2 3 4
5 6 1
5 3 6
3 7 6
2 4 9
2 6
2 4
2 7
1 1
3 2 3 0
1 1
3 6 5 0
1 1
3 3 5 0
3 2 4 0
1 1

예제 출력 1

11
8
8
2

In the first seek, the closest grape is at joint $4$ over the path $1 → 2 → 4$, for a distance of $2 + 9 = 11$. The second seek has the closest grape at joint $7$, while the third seek has both the grapes at joints $6$ and $7$ closest. The fourth and final seek has its closest grape at joint $4$.

This testcase is valid for subtasks 1, 2, 5, and 6.

예제 입력 2

6 11
1 2 3
1 3 4
2 4 1
2 5 4
3 6 6
2 3
1 2
2 4
3 1 3 2
1 1
2 3
3 2 1 2
2 4
1 3
2 2
1 3

예제 출력 2

7
2
-1
4

This testcase is valid for subtasks 1, 3, and 6.

예제 입력 3

7 8
1 2 2
2 3 3
3 6 2
4 6 1
5 6 4
6 7 3
2 3
1 4
2 3
2 5
1 1
3 6 7 4
1 5
1 7

예제 출력 3

3
11
0
8

This testcase is valid for subtasks 1, 4, and 6.

채점 및 기타 정보

  • 예제는 채점하지 않는다.