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

34113번 - Migration Plan 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
7.5 초 2048 MB32121140.741%

문제

JOI Kingdom consists of $N$ cities numbered from 1ドル$ to $N$. There are $N − 1$ one-way roads connecting these cities. Specifically, for each $i = 2, 3, \dots , N,ドル there is a road leading from city $i$ to city $P_i$. Here, it is guaranteed that 1ドル ≤ P_i < i$.

Each of the $N$ cities has a defined danger level. The capital city, city 1ドル,ドル has a danger level of 0ドル$. For city $i$ (2ドル ≤ i ≤ N$), the danger level is defined as the number of roads traversed in the path from city $i$ to city 1ドル$. Due to the structure of JOI Kingdom, there is exactly one unique path from any city $i$ to city 1ドル$.

Currently, there are $K_i$ beavers living in city $i$ (1ドル ≤ i ≤ N$). The president of JOI Kingdom, Bitaro, has planned a beaver relocation program. This relocation plan will be executed over $Q$ days. On the $j$-th day (1ドル ≤ j ≤ Q$), one of the following three types of events will occur:

  • Relocation: All beavers living in a city with danger level $X_j$ at that moment will move to a city with danger level $Y_j,ドル which they can reach by traveling along one or more roads from their current city. It is guaranteed that 0ドル ≤ Y_j < X_j$. Due to the structure of JOI Kingdom, the relocation destination for each beaver is uniquely determined.
  • Immigration: The number of beavers living in city $A_j$ increases by $L_j$ due to immigration from outside JOI Kingdom.
  • Survey: The number of beavers currently living in city $B_j$ is surveyed.

As Bitaro’s subordinate, you realize that you can compute the number of beavers in each survey event based solely on the relocation plan’s information, without physically visiting the city.

Given the structure of JOI Kingdom, the current number of beavers living in each city, and the details of the relocation plan, write a program to compute the results of each survey event.

입력

Read the following data from the standard input.

$N$

$P_2$ $P_3$ $\cdots$ $P_N$

$K_1$ $K_2$ $\cdots$ $K_N$

$Q$

(Query 1ドル$)

(Query 2ドル$)

$\vdots$

(Query $Q$)

Each (Query $j$) (1ドル ≤ j ≤ Q$) consists of several integers separated by spaces. Let the first integer be $T_j,ドル then the content of this line is as follows:

  • If $T_j = 1,ドル the line continues with two integers $X_j,ドル $Y_j$ in this order. This indicates that on day $j,ドル a relocation event occurs, where all beavers living in a city with danger level $X_j$ move to a city with danger level $Y_j$ that they can reach by traveling along one or more roads from their current city.
  • If $T_j = 2,ドル the line continues with two integers $A_j,ドル $L_j$ in this order. This indicates that on day $j,ドル an immigration event occurs, increasing the number of beavers in city $A_j$ by $L_j$.
  • If $T_j = 3,ドル the line continues with one integer $B_j$. This indicates that on day $j,ドル a survey event occurs, where the number of beavers currently living in city $B_j$ is surveyed.

출력

For each $j$ (1ドル ≤ j ≤ Q$) where $T_j = 3,ドル output the number of beavers in city $B_j$ at that moment, one per line, in order.

제한

  • 2ドル ≤ N ≤ 2,円 000,円 000$.
  • 1ドル ≤ P_i < i$ (2ドル ≤ i ≤ N$).
  • 0ドル ≤ K_i ≤ 100$ (1ドル ≤ i ≤ N$).
  • 1ドル ≤ Q ≤ 2,円 000,円 000$.
  • $T_j$ is either 1ドル,ドル 2ドル,ドル or 3ドル$ (1ドル ≤ j ≤ Q$).
  • If $T_j = 1,ドル then 0ドル ≤ Y_j < X_j ≤ N − 1$ (1ドル ≤ j ≤ Q$).
  • If $T_j = 2,ドル then 1ドル ≤ A_j ≤ N,ドル 1ドル ≤ L_j ≤ 100$ (1ドル ≤ j ≤ Q$).
  • If $T_j = 3,ドル then 1ドル ≤ B_j ≤ N$ (1ドル ≤ j ≤ Q$).
  • At least one $j$ (1ドル ≤ j ≤ Q$) satisfies $T_j = 3$.
  • All input values are integers.

서브태스크

Let the maximum danger level of the cities be $D$.

번호배점제한
14

$D = 1$.

28

$N ≤ 20$.

313

$D ≤ 20$.

415

No queries satisfy $T_j = 2,ドル and at most 5ドル$ queries satisfy $T_j = 3$.

515

At most 5ドル$ queries satisfy $T_j = 3$.

627

No queries satisfy $T_j = 2$.

718

No additional constraints.

예제 입력 1

4
1 1 2
1 3 4 3
6
3 1
1 1 0
3 1
3 2
1 2 1
3 2

예제 출력 1

1
8
0
3

Initially, cities 1ドル,ドル 2ドル,ドル 3ドル,ドル 4ドル$ have 1ドル,ドル 3ドル,ドル 4ドル,ドル 3ドル$ beavers respectively. The danger levels of these cities are 0ドル,ドル 1ドル,ドル 1ドル,ドル 2ドル,ドル respectively.

  • On day 1ドル,ドル a survey event occurs. Output 1ドル$ on the first line, representing the number of beavers in city 1ドル$.
  • On day 2ドル,ドル a relocation event occurs. All beavers in city 2ドル$ and city 3ドル$ move to city 1ドル$. At the end of day 2ドル,ドル cities 1ドル,ドル 2ドル,ドル 3ドル,ドル 4ドル$ contain 8ドル,ドル 0ドル,ドル 0ドル,ドル 3ドル$ beavers, respectively.
  • On day 3ドル,ドル a survey event occurs. Output 8ドル$ on the second line.
  • On day 4ドル,ドル a survey event occurs. Output 0ドル$ on the third line.
  • On day 5ドル,ドル a relocation event occurs. All beavers in city 4ドル$ move to city 2ドル$. At the end of day 5ドル,ドル cities 1ドル,ドル 2ドル,ドル 3ドル,ドル 4ドル$ contain 8ドル,ドル 3ドル,ドル 0ドル,ドル 0ドル$ beavers, respectively.
  • On day 6ドル,ドル a survey event occurs. Output 3ドル$ on the fourth line.

This input example satisfies the constraints of subtasks 2, 3, 4, 5, 6, 7.

예제 입력 2

3
1 1
3 1 4
11
2 2 5
1 2 0
3 1
1 1 0
3 1
3 2
2 3 4
3 3
1 1 0
3 3
3 1

예제 출력 2

3
13
0
4
0
1

Initially, cities 1ドル,ドル 2ドル,ドル 3ドル$ have 3ドル,ドル 1ドル,ドル 4ドル$ beavers, respectively. The danger levels of these cities are 0ドル,ドル 1ドル,ドル 1ドル,ドル respectively.

  • On day 1ドル,ドル an immigration event occurs. The number of beavers in city 2ドル$ increases by 5ドル$. At the end of day 1ドル,ドル cities 1ドル,ドル 2ドル,ドル 3ドル$ contain 3ドル,ドル 6ドル,ドル 4ドル$ beavers, respectively.
  • On day 2ドル,ドル a relocation event occurs. No beavers move, as no city has a danger level of 2ドル$.
  • On day 3ドル,ドル a survey event occurs. Output 3ドル$ on the first line.
  • On day 4ドル,ドル a relocation event occurs. All beavers in city 2ドル$ and city 3ドル$ move to city 1ドル$. At the end of day 4ドル,ドル cities 1ドル,ドル 2ドル,ドル 3ドル$ contain 13ドル,ドル 0ドル,ドル 0ドル$ beavers, respectively.
  • On day 5ドル,ドル a survey event occurs. Output 13ドル$ on the second line.
  • On day 6ドル,ドル a survey event occurs. Output 0ドル$ on the third line.

Subsequent events occur similarly, but their descriptions are omitted.

This input example satisfies the constraints of subtasks 1, 2, 3, 7.

예제 입력 3

7
1 2 1 3 3 2
5 2 8 9 4 0 5
10
1 3 1
2 4 10
3 2
1 6 3
1 2 0
3 1
3 4
2 5 6
3 5
3 3

예제 출력 3

6
18
19
6
0

This input example satisfies the constraints of subtasks 2, 3, 5, 7.

힌트

출처

Camp > JOI Spring Training Camp > JOI 2024/2025 Spring Training Camp 4-2번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.
(追記) (追記ここまで)

출처

대학교 대회

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

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