Wesley's Anger Contest 1 Problem 7 - Arithmetic Subtrees

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 3.0s
Memory limit: 256M

Author:
Problem types

After having lots of fun dealing with arithmetic sequences and squares, you decided to try combining arithmetic sequences and subtrees!

You are given a tree with N vertices numbered from 0 to N1, rooted at vertex 0. Recall that a tree is a connected graph where there is exactly one path between any two vertices. It can be seen that each vertex has a unique parent Pi for all vertices i (1iN1). For simplicity, we will assume that 0Pi<i for all i (1iN1).

Let d(i) be the depth of vertex i and d(0)=0. Then d(i)=d(Pi)+1 for all i (1iN1). We define the subtree of vertex u to be all vertices v where d(u)d(v) and u is on the unique path from vertex 0 to vertex v. Each vertex has a value, initially equal to 0.

You are asked to perform Q operations on the tree, which come in two forms.

Input Format Description
UPDATE u a b c For all vertices v in the subtree of u with ad(v)b, add c×(d(v)d(u)) to the value of vertex v
QUERY u a b Output the sum of the values of all vertices v in the subtree of u with ad(v)b

Constraints

For this problem, you will NOT be required to pass all the samples in order to receive points. In addition, all subtasks are disjoint, and you are NOT required to pass previous subtasks to earn points for a specific subtask.

Subtask Points N,Q Additional Constraints
1 5% 2N1000
1Q1000
None
2 10% 2N100000
1Q100000
Pi=i1
3 10% 2N100000
1Q100000
a=0 and b=N1 for all operations
4 10% 2N100000
1Q100000
u=0 for all operations
5 65% 2N100000
1Q100000
None

For all subtasks:

0Pi<i for all i (1iN1)

0u,a,bN1 for all operations

0c<1000 for all operations

Input Specification

The first line contains 2 integers, N and Q.

The next line contains N1 integers: P1,P2,,PN1, the parent of each vertex.

The next Q lines each contain a valid operation as described above.

Output Specification

This problem is graded with an identical checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n character and that there are no trailing spaces.

For each QUERY operation, output the answer on its own line. If there are no vertices that meet the query parameters, then the answer is 0.

Sample Input 1

Copy
5 4
0 1 1 3
UPDATE 0 1 2 397
QUERY 0 2 2
UPDATE 3 0 3 688
QUERY 1 1 3

Sample Output 1

Copy
1588
2673

Sample Explanation 1

After the first UPDATE, the values of the vertices are [0,397,794,794,0].

After the second UPDATE, the values of the vertices are [0,397,794,794,688].

Sample Input 2

Copy
5 4
0 1 2 3
UPDATE 2 1 4 541
QUERY 0 1 4
UPDATE 0 1 3 134
QUERY 0 4 1

Sample Output 2

Copy
1623
0

Sample Explanation 2

After the first UPDATE, the values of the vertices are [0,0,0,541,1082].

After the second UPDATE, the values of the vertices are [0,134,268,943,1082].

Sample Input 3

Copy
5 4
0 1 1 3
UPDATE 3 0 4 544
QUERY 4 0 4
UPDATE 1 0 4 55
QUERY 0 0 4

Sample Output 3

Copy
544
764

Sample Explanation 3

After the first UPDATE, the values of the vertices are [0,0,0,0,544].

After the second UPDATE, the values of the vertices are [0,0,55,55,654].

Sample Input 4

Copy
5 4
0 1 1 3
UPDATE 0 1 2 667
QUERY 0 0 1
UPDATE 0 2 3 617
QUERY 0 2 3

Sample Output 4

Copy
667
6987

Sample Explanation 4

After the first UPDATE, the values of the vertices are [0,667,1334,1334,0].

After the second UPDATE, the values of the vertices are [0,667,2568,2568,1851].


Comments

There are no comments at the moment.