Tree Beauty
Time limit2sMemory limit512 MB
Given a rooted tree, apply updates that add floor(Y/K^depth) to a subtree and answer subtree sum queries. N and Q are up to 100,000.
- Level
Hard9 of 10
- Topics
- Tree, Segment tree, Divide and conquer, Math
- Solved
- No attempts yet
Problem
A rooted tree has N vertices numbered from 1 to N. Vertex 1 is the root, and is the parent of vertex for all . Each vertex has a beautiness value, which starts at 0.
A special machine increases the beautiness values of vertices. When you give the machine three parameters X, Y, and K, it increases the beautiness value of every vertex in the subtree of vertex X. If vertex is in the subtree of vertex X, its beautiness value increases by , where d is the number of edges on the path from X to . For example, vertex X increases by Y, the children of X increase by , the children of those children increase by , and so on.
You perform Q operations one at a time. Each operation is one of the following two types.
- Use the machine with parameters X, Y, and K.
- Report the total beautiness value of all vertices in the subtree of vertex X.
Input
The first line contains two integers N and Q (), the number of vertices and the number of operations. The next line contains N - 1 integers (), the parent of each vertex . Each of the next Q lines is one of the following.
- 1 X Y K (; ): use the machine with parameters X, Y, and K.
- 2 X (): report the total beautiness value of the subtree of vertex X.
Output
For each operation of the second type, in the order given in the input, output on its own line the total beautiness value of all vertices in the subtree of vertex X.