#P15949. [JOI Final 2026] JOI 国的节日 3 / Festivals in JOI Kingdom 3

[JOI Final 2026] JOI 国的节日 3 / Festivals in JOI Kingdom 3

Problem Description

JOI Kingdom consists of NN cities and N1N-1 highways. The cities are numbered 11 through NN, and the highways are numbered 11 through N1N-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 ii (1iN1 \leq i \leq N) is initially CiC_i. Each highway has a travel time, represented by a positive integer. Highway jj (1jN11 \leq j \leq N-1) connects city AjA_j and city BjB_j, and its travel time is initially DjD_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 uu is adjacent to city vv 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 vv and uu, the parade from city vv reaches city uu at time t+dt + d, where tt is the time when the cauldron of city vv is ignited, and dd is the travel time of the highway connecting cities vv and uu.

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 00 be the start of the festival. For the city ii whose popularity is cc, the time when city ii ignites its cauldron is determined as follows:

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

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

Event kk (1kQ1 \leq k \leq Q) is one of the following 33 types:

  • Type 11: The popularity of city VkV_k changes to XkX_k.
  • Type 22: The travel time of highway EkE_k changes to XkX_k.
  • Type 33: Mr. K visits city VkV_k. Assuming a festival starts at this moment, you must determine whether city VkV_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 33 event, determines when the city Mr. K visits ignites its cauldron.

Input Format

Read the following data from the standard input.

NN
A1B1D1A_1 B_1 D_1
\vdots
AN1BN1DN1A_{N-1} B_{N-1} D_{N-1}
C1C_1
\vdots
CNC_N
QQ
(Query 11)
\vdots
(Query QQ)

(Query kk) represents the details of event kk (1kQ1 \leq k \leq Q). In (Query kk), space-separated integers are written. Let PkP_k be the first integer. PkP_k is 11, 22, or 33, which means the type of event kk. Then (Query kk) means as follows:

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

Output Format

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

  • If the city Mr. K visits would ignite its cauldron, output the time that the cauldron is ignited.
  • Otherwise, output -1\texttt{-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
80
70
60
-1
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
30
20
10
30
-1

Hint

Sample 1

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

  • At time 00, cities 3,4,5,73, 4, 5, 7 ignite their cauldrons.
  • At time 5050, city 22 ignites its cauldron. By then, the parades from cities 3,5,73, 5, 7 have reached city 22.
  • At time 8080, city 11 ignites its cauldron. By then, the parades from cities 2,42, 4 have reached city 11.
  • At time 9090, city 66 ignites its cauldron. By then, the parade from city 11 has reached city 66. Since city 11 would ignite its cauldron at time 8080, output 8080.

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

  • At time 00, cities 3,4,5,6,73, 4, 5, 6, 7 ignite their cauldrons.
  • At time 5050, city 22 ignites its cauldron. By then, the parades from cities 3,5,73, 5, 7 have reached city 22.
  • At time 7070, city 11 ignites its cauldron. By then, the parades from cities 4,64, 6 have reached city 11. Since city 11 would ignite its cauldron at time 7070, output 7070.

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

  • At time 00, cities 3,4,5,6,73, 4, 5, 6, 7 ignite their cauldrons.
  • At time 3030, city 22 ignites its cauldron. By then, the parades from cities 3,5,73, 5, 7 have reached city 22.
  • At time 6060, city 11 ignites its cauldron. By then, the parades from cities 2,62, 6 have reached city 11. Since city 11 would ignite its cauldron at time 6060, output 6060.

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

  • At time 00, cities 3,4,5,73, 4, 5, 7 ignite their cauldrons. Cities 1,2,61, 2, 6 would never ignite their cauldrons. Since city 11 would never ignite its cauldron, output 1-1. This sample input satisfies the constraints of Subtasks 1,3,5,71, 3, 5, 7.

Sample 2

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

Constraints

  • 2N1500002 \le N \le 150000.
  • 0CiN0 \le C_i \le N (1iN1 \le i \le N).
  • 1Aj<BjN1 \le A_j < B_j \le N (1jN11 \le j \le N-1).
  • 1Dj10000001 \le D_j \le 1000000 (1jN11 \le j \le N-1).
  • It is possible to travel from any city to any other city by traversing a number of highways.
  • 1Q1500001 \le Q \le 150000.
  • If Pk=1P_k = 1, we have 1VkN1 \le V_k \le N, 0XkN0 \le X_k \le N (1kQ1 \le k \le Q).
  • If Pk=2P_k = 2, we have 1EkN11 \le E_k \le N-1, 1Xk10000001 \le X_k \le 1000000 (1kQ1 \le k \le Q).
  • If Pk=3P_k = 3, we have 1VkN1 \le V_k \le N (1kQ1 \le k \le Q).
  • Given values are all integers.

Subtasks

  1. (6 points) N2000N \le 2000, Q2000Q \le 2000.
  2. (7 points) Aj=1A_j = 1, Bj=j+1B_j = j+1 (1jN11 \le j \le N-1). If Pk=3P_k = 3, we have Vk=1V_k = 1 (1kQ1 \le k \le Q).
  3. (14 points) N1N-1 is divisible by 33. Aj=((j1)modN13)+1A_j = ((j-1) \mod \frac{N-1}{3}) + 1, Bj=j+1B_j = j+1 (1jN11 \le j \le N-1). If Pk=3P_k = 3, we have Vk=1V_k = 1 (1kQ1 \le k \le Q).
  4. (25 points) Pk1P_k \ne 1. If Pk=3P_k = 3, we have Vk=1V_k = 1 (1kQ1 \le k \le Q).
  5. (12 points) If Pk=3P_k = 3, we have Vk=1V_k = 1 (1kQ1 \le k \le Q).
  6. (22 points) Pk1P_k \ne 1 (1kQ1 \le k \le Q).
  7. (14 points) No additional constraints.