K - JOI 国のお祭り事情 3 (Festivals in JOI Kingdom 3) 解説 /

実行時間制限: 4 sec / メモリ制限: 1024 MiB

Score : 100 points

Problem Statement

JOI Kingdom consists of N cities and N-1 highways. The cities are numbered 1 through N, and the highways are numbered 1 through N-1. It is possible to travel from any city to any other city by traversing a number of highways.

Each city has a popularity, represented by a non-negative integer. The popularity of city i (1 \leq i \leq N) is initially C_i. Each highway has a travel time, represented by a positive integer. Highway j (1 \leq j \leq N-1) connects city A_j and city B_j, and its travel time is initially D_j.

Each city in JOI Kingdom has a cauldron. JOI Kingdom keeps its tradition that, in a festival, cities ignite their cauldrons, and these ignitions signal the departure of parades from those cities.

City u is adjacent to city v if these two cities are directly connected by a highway. At the exact moment when a city ignites its cauldron, one parade will depart from this city for each of its adjacent cities, spending time equal to the travel time of the corresponding highway. To be precise, for two adjacent cities v and u, the parade from city v reaches city u at time t+d, where t is the time when the cauldron of city v is ignited, and d is the travel time of the highway connecting cities v and u.

Some cities ignite their cauldron the moment the festival starts, while other cities only do so after the festival heats up enough. Let the time 0 be the start of the festival. For the city i whose popularity is c, the time when city i ignites its cauldron is determined as follows:

  • If c = 0, city i ignites its cauldron at time 0.
  • If c \geq 1, city i ignites its cauldron at the time when the number of parades that have arrived from adjacent cities becomes at least c. If this never happens, city i never ignites its cauldron.

Mr. K will stay at JOI Kingdom. During his stay, JOI Kingdom will have Q events related to its festival. These events are numbered 1 through Q from earliest to latest.

Event k (1 \leq k \leq Q) is one of the following 3 types:

  • Type 1: The popularity of city V_k changes to X_k.
  • Type 2: The travel time of highway E_k changes to X_k.
  • Type 3: Mr. K visits city V_k. Assuming a festival starts at this moment, you must determine whether {city \nolinebreak V_k} would ignite its cauldron, and if so, calculate the time that the cauldron is ignited.

Write a program which, given the structure of JOI Kingdom, popularity of each city, travel time of each highway, and details of the events, for each Type 3 event, determines when the city Mr. K visits ignites its cauldron.


Input

Read the following data from the standard input.

N
A_1 B_1 D_1
\vdots
A_{N-1} B_{N-1} D_{N-1}
C_1
\vdots
C_N
Q
(Query 1)
\vdots
(Query Q)

(Query k) represents the details of event k (1 \leq k \leq Q). In (Query k), space-separated integers are written. Let P_k be the first integer. P_k is 1, 2, or 3, which means the type of event k. Then (Query k) means as follows:

  • If P_k = 1, there are two more integers V_k, X_k written in this order. This means that the popularity of city V_k changes to X_k.
  • If P_k = 2, there are two more integers E_k, X_k written in this order. This means that the travel time of highway E_k changes to X_k.
  • If P_k = 3, there is one more integer V_k written. This means Mr. K visits city V_k and, assuming a festival starts at this moment, you must determine the time that the cauldron at city V_k is ignited.

Output

To standard output, output the following in one line for each event k (1 \leq k \leq Q) with P_k = 3, in the increasing order of k.

  • If the city Mr. K visits would ignite its cauldron, output the time that the cauldron is ignited.
  • Otherwise, output -1.

Constraints

  • 2 \leq N \leq 150\,000.
  • 0 \leq C_i \leq N (1 \leq i \leq N).
  • 1 \leq A_j < B_j \leq N (1 \leq j \leq N-1).
  • 1 \leq D_j \leq 1\,000\,000 (1 \leq j \leq N-1).
  • It is possible to travel from any city to any other city by traversing a number of highways.
  • 1 \leq Q \leq 150\,000.
  • If P_k = 1, we have 1 \leq V_k \leq N, 0 \leq X_k \leq N (1 \leq k \leq Q).
  • If P_k = 2, we have 1 \leq E_k \leq N-1, 1 \leq X_k \leq 1\,000\,000 (1 \leq k \leq Q).
  • If P_k = 3, we have 1 \leq V_k \leq N (1 \leq k \leq Q).
  • Given values are all integers.

Subtasks

  1. (6 points) N \leq 2\,000, Q \leq 2\,000.
  2. (7 points) A_j = 1, B_j = j+1 (1 \leq j \leq N-1). If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  3. (14 points) N-1 is divisible by 3. A_j = ((j - 1) \bmod \frac{N-1}{3}) + 1, B_j = j+1 (1 \leq j \leq N-1). If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  4. (25 points) P_k \neq 1. If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  5. (12 points) If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  6. (22 points) P_k \neq 1 (1 \leq k \leq Q).
  7. (14 points) No additional constraints.

Sample Input 1

7
1 2 30
2 3 30
1 4 70
2 5 20
1 6 10
2 7 50
2
3
0
0
0
1
0
8
3 1
1 6 0
3 1
2 6 10
3 1
1 2 7
1 6 7
3 1

Sample Output 1

80
70
60
-1

In the festival considered in event 1, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,7 ignite their cauldrons.
  • At time 50, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 80, city 1 ignites its cauldron. By then, the parades from cities 2,4 have reached city 1.
  • At time 90, city 6 ignites its cauldron. By then, the parade from city 1 has reached city 6.

Since city 1 would ignite its cauldron at time 80, output 80.

In the festival considered in event 3, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,6,7 ignite their cauldrons.
  • At time 50, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 70, city 1 ignites its cauldron. By then, the parades from cities 4,6 have reached city 1.

Since city 1 would ignite its cauldron at time 70, output 70.

In the festival considered in event 5, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,6,7 ignite their cauldrons.
  • At time 30, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 60, city 1 ignites its cauldron. By then, the parades from cities 2,6 have reached city 1.

Since city 1 would ignite its cauldron at time 60, output 60.

In the festival considered in event 8, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,7 ignite their cauldrons.

Cities 1,2,6 would never ignite their cauldrons. Since city 1 would never ignite its cauldron, output -1.

This sample input satisfies the constraints of Subtasks 1,3,5,7.


Sample Input 2

6
1 2 10
1 3 30
1 4 50
1 5 30
1 6 10
2
0
0
0
0
1
10
3 1
2 3 20
3 1
1 6 0
3 1
1 1 4
3 1
1 2 6
1 3 6
3 1

Sample Output 2

30
20
10
30
-1

This sample input satisfies the constraints of Subtasks 1,2,5,7.

配点: 100

問題文

JOI 国は N 個の街と N-1 本の国道からなり, 街には 1 から N までの番号が,国道には 1 から N-1 までの番号が付けられている. どの街からどの街へも,何本かの国道を通ることで移動できる.

それぞれの街は,非負整数で表される人気度をもつ. 街 i (1 \leqq i \leqq N) の人気度ははじめ C_i である. それぞれの国道は,正の整数で表される所要時間をもつ. 国道 j (1 \leqq j \leqq N-1) は街 A_j と街 B_j を双方向につないでおり,その所要時間ははじめ D_j である.

JOI 国の街には聖火台が 1 つずつ設置されている. JOI 国のお祭りでは,街の聖火台に火をつけ,それを合図に街からパレードを出発させる伝統がある.

v と街 u が国道で直接つながれているとき,街 u は街 v隣接するという. ある街の聖火台に火がつけられた時点で,隣接する街のそれぞれに向けて 1 つずつパレードが出発し, それが通る国道の所要時間と同じ時間をかけて歩いたあと,向かいの街に到着する. つまり,互いに隣接する街 v, u について,街 v の聖火台に火がつけられた時刻を t,街 v, u を直接つなぐ国道の所要時間を d とすると, 街 v から出発するパレードが街 u に到着する時刻は t+d である.

街によっては,お祭りが始まった瞬間に火をつけるところもあれば,お祭りが盛り上がってきたタイミングで火をつけるところもある. お祭りが始まるタイミングを時刻 0 とする. 街 i の聖火台に火をつける時刻は,街 i の人気度を c とすると,以下のように決定される.

  • c = 0 の場合,時刻 0 に火をつける.
  • c \geqq 1 の場合,隣接する街から来て到着したパレードが c 個以上にはじめてなった時刻に火をつける. そのようなことが起こらない場合,火はつけない.

これから K 理事長が JOI 国に滞在する. そのあいだに, JOI 国のお祭りについて Q 回の出来事がある. これらの出来事には,起きるのが早い順に 1 から Q までの番号が付けられている. 出来事 k (1 \leqq k \leqq Q)は以下の 3 種類のいずれかである.

  • タイプ 1: 街 V_k の人気度が X_k に変更される.
  • タイプ 2: 国道 E_k の所要時間が X_k に変更される.
  • タイプ 3: K 理事長が街 V_k に来る.このとき,仮にこの時点でお祭りが始まるとして,街 V_k の聖火台に火がつけられるかどうかを判定し,つけられる場合はその時刻を求めなければならない.

JOI 国の構造,街の人気度,国道の所要時間,および滞在中の出来事の情報が与えられたとき, タイプ 3 の出来事において K 理事長がいる街の聖火台に火がつけられる時刻を計算するプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N
A_1 B_1 D_1
\vdots
A_{N-1} B_{N-1} D_{N-1}
C_1
\vdots
C_N
Q
(Query 1)
\vdots
(Query Q)

(Query k) は出来事 k の情報を表す(1 \leqq k \leqq Q). (Query k) にはいくつかの整数が空白区切りで書かれている. そのうち最初に書かれているものは出来事の種類を表す整数であり,1,2,3 のいずれかである. これを P_k とすると,この行の内容は以下の 3 種類のいずれかである.

  • P_k = 1 のとき,この行には続いて 2 個の整数 V_k, X_k がこの順に書かれている. これは,街 V_k の人気度が X_k に変更されることを表す.
  • P_k = 2 のとき,この行には続いて 2 個の整数 E_k, X_k がこの順に書かれている. これは,国道 E_k の所要時間が X_k に変更されることを表す.
  • P_k = 3 のとき,この行には続いて整数 V_k が書かれている. これは,K 理事長がこのとき街 V_k におり,仮にこの時点でお祭りが始まるとしたときに街 V_k の聖火台に火がつけられる時刻を求める必要があることを表す.

出力

P_k = 3 である出来事 k (1 \leqq k \leqq Q) それぞれに対して, K 理事長がいる街の聖火台に火がつけられる場合はその時刻を, 火がつけられない場合は -1 を, k の昇順に改行区切りで出力せよ.


制約

  • 2 \leqq N \leqq 150\,000
  • 0 \leqq C_i \leqq N (1 \leqq i \leqq N).
  • 1 \leqq A_j < B_j \leqq N (1 \leqq j \leqq N-1).
  • 1 \leqq D_j \leqq 1\,000\,000 (1 \leqq j \leqq N-1).
  • どの街からどの街へも,何本かの国道を通ることで移動できる.
  • 1 \leqq Q \leqq 150\,000
  • P_k = 1 のとき,1 \leqq V_k \leqq N0 \leqq X_k \leqq N (1 \leqq k \leqq Q).
  • P_k = 2 のとき,1 \leqq E_k \leqq N-11 \leqq X_k \leqq 1\,000\,000 (1 \leqq k \leqq Q).
  • P_k = 3 のとき,1 \leqq V_k \leqq N (1 \leqq k \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (6 点) N \leqq 2\,000Q \leqq 2\,000
  2. (7 点) A_j = 1B_j = j+1 (1 \leqq j \leqq N-1). P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  3. (14 点) N-13 の倍数. A_j = ((j - 1) \bmod \frac{N-1}{3}) + 1B_j = j+1 (1 \leqq j \leqq N-1). P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  4. (25 点) P_k \neq 1P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  5. (12 点) P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  6. (22 点) P_k \neq 1 (1 \leqq k \leqq Q).
  7. (14 点) 追加の制約はない.

入力例 1

7
1 2 30
2 3 30
1 4 70
2 5 20
1 6 10
2 7 50
2
3
0
0
0
1
0
8
3 1
1 6 0
3 1
2 6 10
3 1
1 2 7
1 6 7
3 1

出力例 1

80
70
60
-1

出来事 1 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,7 の聖火台に火がつく.
  • 時刻 50 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 80 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 2,4 からパレードが到着している.
  • 時刻 90 に,街 6 の聖火台に火がつく.この時点で,街 6 には 街 1 からパレードが到着している.

1 の聖火台に火がつく時刻は 80 なので,80 を出力する.

出来事 3 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,6,7 の聖火台に火がつく.
  • 時刻 50 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 70 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 4,6 からパレードが到着している.

1 の聖火台に火がつく時刻は 70 なので,70 を出力する.

出来事 5 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,6,7 の聖火台に火がつく.
  • 時刻 30 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 60 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 2,6 からパレードが到着している.

1 の聖火台に火がつく時刻は 60 なので,60 を出力する.

出来事 8 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,7 の聖火台に火がつく.

1,2,6 の聖火台に火がつくことはない.街 1 の聖火台に火がつくことはないので,-1 を出力する.

この入力例は小課題 1,3,5,7 の制約を満たす.


入力例 2

6
1 2 10
1 3 30
1 4 50
1 5 30
1 6 10
2
0
0
0
0
1
10
3 1
2 3 20
3 1
1 6 0
3 1
1 1 4
3 1
1 2 6
1 3 6
3 1

出力例 2

30
20
10
30
-1

この入力例は小課題 1,2,5,7 の制約を満たす.