#P15600. [ICPC 2020 Jakarta R] Tree Beauty

[ICPC 2020 Jakarta R] Tree Beauty

题目描述

There is a rooted tree of NN vertices, numbered from 11 to NN. Vertex 11 is the root of the tree, while PiP_i is the parent of vertex ii, for all 2iN2 \leq i \leq N. Each vertex has a beautiness value, which is initially 00.

You can use a special machine that can increase the beautiness value of the vertices. By giving three parameters XX, YY, and KK to the machine, the machine will increase the beautiness value of all vertices in the subtree of vertex XX. If vertex XX' is in the subtree of vertex XX, then its beautiness value will increase by YKd\left\lfloor \frac{Y}{K^d} \right\rfloor, where dd is the number of edges in the path connecting vertex XX to vertex XX'. For example, the beautiness value of vertex XX will increase by YY, the beautiness value of all children of vertex XX will increase by YK\left\lfloor \frac{Y}{K} \right\rfloor, the beautiness value of all children of vertex XX's children will increase by YK2\left\lfloor \frac{Y}{K^2} \right\rfloor, and so on.

You are going to perform QQ operations one by one. Each operation is one of the following types:

  1. Use the special machine by giving three parameters XX, YY, and KK to the machine.
  2. Report the total beautiness value of all vertices in the subtree of vertex XX.

For each operation of the second type, output the total beautiness value of all vertices in the subtree of vertex XX.

输入格式

Input begins with a line containing two integers: N QN\ Q (1N,Q1000001 \leq N, Q \leq 100\,000) representing the number of vertices and the number of operations, respectively. The next line contains N1N-1 integers: PiP_i (1Pi<i1 \leq P_i < i) representing the parent of vertices i[2,N]i \in [2, N]; note that ii starts from 22. The next QQ lines each contains one of the following input format representing the operation you should perform.

  • 1 X Y K1\ X\ Y\ K (1XN1 \leq X \leq N; 1Y,K1000001 \leq Y, K \leq 100\,000)
    Use the special machine by giving three parameters XX, YY, and KK to the machine.
  • 2 X2\ X (1XN1 \leq X \leq N)
    Output the total beautiness value of all vertices in the subtree of vertex XX.

There will be at least one operation of the second type.

输出格式

For each operation of the second type in the same order as input, output in a line an integer representing the total beautiness value of all vertices in the subtree of vertex XX.

5 5
1 1 3 3
1 1 99 2
2 1
2 3
1 3 2 3
2 3

245
97
99

提示

Explanation for the sample input/output #1

The tree is illustrated by the following image.

:::align{center} :::

  • The first operation increases the beautiness values of vertex 11 by 9999, vertex 22 and 33 by 4949, and vertex 44 and 55 by 2424.
  • The total beautiness value of all vertices in the subtree of vertex 11 is 99+49+49+24+24=24599 + 49 + 49 + 24 + 24 = 245.
  • The total beautiness value of all vertices in the subtree of vertex 33 is 49+24+24=9749 + 24 + 24 = 97.
  • The fourth operation increases the beautiness values of vertex 33 by 22 and vertex 44 and 55 by 00.
  • The total beautiness value of all vertices in the subtree of vertex 33 is 51+24+24=9951 + 24 + 24 = 99.