logo AlgoBeat OnlineJudge
登录 注册

#215662. [ICPC 2020 Jakarta R] Tree Beauty

内存限制:256 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

There is a rooted tree of vertices, numbered from to . Vertex is the root of the tree, while is the parent of vertex , for all . Each vertex has a beautiness value, which is initially .

You can use a special machine that can increase the beautiness value of the vertices. By giving three parameters , , and to the machine, the machine will increase the beautiness value of all vertices in the subtree of vertex . If vertex is in the subtree of vertex , then its beautiness value will increase by , where is the number of edges in the path connecting vertex to vertex . For example, the beautiness value of vertex will increase by , the beautiness value of all children of vertex will increase by , the beautiness value of all children of vertex 's children will increase by , and so on.

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

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

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

输入格式

Input begins with a line containing two integers: () representing the number of vertices and the number of operations, respectively. The next line contains integers: () representing the parent of vertices ; note that starts from . The next lines each contains one of the following input format representing the operation you should perform.

  • (; )
    Use the special machine by giving three parameters , , and to the machine.
  • ()
    Output the total beautiness value of all vertices in the subtree of vertex .

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 .

样例

样例输入 1

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

样例输出 1

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 by , vertex and by , and vertex and by .
  • The total beautiness value of all vertices in the subtree of vertex is .
  • The total beautiness value of all vertices in the subtree of vertex is .
  • The fourth operation increases the beautiness values of vertex by and vertex and by .
  • The total beautiness value of all vertices in the subtree of vertex is .