Migration Plan

시간 제한7.5초메모리 제한2048 MB

요약
위험도로 정의된 트리 깊이를 기준으로 한 도시 사이에서 비버 무리가 이동하며, 같은 위험도의 모든 비버를 상위 위험도 도시로 옮기는 이주, 한 도시에 비버를 더하는 이민, 한 도시의 비버 수를 묻는 조사를 온라인으로 처리한다.
난이도

어려움10점 중 9점

유형
트리, 동적 계획법, 구현, 그리디
정답자
아직 제출이 없습니다

문제

JOI Kingdom consists of NN cities numbered from 11 to NN. There are N−1N − 1 one-way roads connecting these cities. Specifically, for each i=2,3,…,Ni = 2, 3, \dots , N, there is a road leading from city ii to city P_iP\_i. Here, it is guaranteed that 1≤P_i<i1 ≤ P\_i < i.

Each of the NN cities has a defined danger level. The capital city, city 11, has a danger level of 00. For city ii (2≤i≤N2 ≤ i ≤ N), the danger level is defined as the number of roads traversed in the path from city ii to city 11. Due to the structure of JOI Kingdom, there is exactly one unique path from any city ii to city 11.

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

  • Relocation: All beavers living in a city with danger level X_jX\_j at that moment will move to a city with danger level Y_jY\_j, which they can reach by traveling along one or more roads from their current city. It is guaranteed that 0≤Y_j<X_j0 ≤ 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_jA\_j increases by L_jL\_j due to immigration from outside JOI Kingdom.
  • Survey: The number of beavers currently living in city B_jB\_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.

NN

P_2P\_2 P_3P\_3 ⋯\cdots P_NP\_N

K_1K\_1 K_2K\_2 ⋯\cdots K_NK\_N

QQ

(Query 11)

(Query 22)

⋮\vdots

(Query QQ)

Each (Query jj) (1≤j≤Q1 ≤ j ≤ Q) consists of several integers separated by spaces. Let the first integer be T_jT\_j, then the content of this line is as follows:

  • If T_j=1T\_j = 1, the line continues with two integers X_jX\_j, Y_jY\_j in this order. This indicates that on day jj, a relocation event occurs, where all beavers living in a city with danger level X_jX\_j move to a city with danger level Y_jY\_j that they can reach by traveling along one or more roads from their current city.
  • If T_j=2T\_j = 2, the line continues with two integers A_jA\_j, L_jL\_j in this order. This indicates that on day jj, an immigration event occurs, increasing the number of beavers in city A_jA\_j by L_jL\_j.
  • If T_j=3T\_j = 3, the line continues with one integer B_jB\_j. This indicates that on day jj, a survey event occurs, where the number of beavers currently living in city B_jB\_j is surveyed.

출력

For each jj (1≤j≤Q1 ≤ j ≤ Q) where T_j=3T\_j = 3, output the number of beavers in city B_jB\_j at that moment, one per line, in order.

제한

  • 2≤N≤2,000,0002 ≤ N ≤ 2\\, 000\\, 000.
  • 1≤P_i<i1 ≤ P\_i < i (2≤i≤N2 ≤ i ≤ N).
  • 0≤K_i≤1000 ≤ K\_i ≤ 100 (1≤i≤N1 ≤ i ≤ N).
  • 1≤Q≤2,000,0001 ≤ Q ≤ 2\\, 000\\, 000.
  • T_jT\_j is either 11, 22, or 33 (1≤j≤Q1 ≤ j ≤ Q).
  • If T_j=1T\_j = 1, then 0≤Y_j<X_j≤N−10 ≤ Y\_j < X\_j ≤ N − 1 (1≤j≤Q1 ≤ j ≤ Q).
  • If T_j=2T\_j = 2, then 1≤A_j≤N1 ≤ A\_j ≤ N, 1≤L_j≤1001 ≤ L\_j ≤ 100 (1≤j≤Q1 ≤ j ≤ Q).
  • If T_j=3T\_j = 3, then 1≤B_j≤N1 ≤ B\_j ≤ N (1≤j≤Q1 ≤ j ≤ Q).
  • At least one jj (1≤j≤Q1 ≤ j ≤ Q) satisfies T_j=3T\_j = 3.
  • All input values are integers.

예제3

  1. 예제 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
    8
    0
    3
    
  2. 예제 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
    
    예상 출력
    3
    13
    0
    4
    0
    1
    
  3. 예제 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
    
    예상 출력
    6
    18
    19
    6
    0