Logo
(追記) (追記ここまで)

27291번 - Grapevine 서브태스크다국어

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

문제

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.

힌트

출처

Olympiad > National Olympiad in Informatics (Singapore) > NOI 2022 4번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /