Under construction

Still being built. Things may change

Namunamu, Time to Plant Trees

Time limit2sMemory limit512 MB

Summary
Given a rooted tree with fruit weights, process queries that graft a new weighted vertex onto a vertex and report the fruit weight sum on the path from a vertex to the root.
Level

Medium5 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

Geunseong is fond of trees.

He is a developer now, but his love for trees has not faded. One day, while playing with binary trees, Geunseong realized that a binary tree is a tree yet bears no fruit, and he was deeply shocked. Insisting that trees must bear fruit, he built a tree graph that bears fruit and named it "Namunamu".

The features of Namunamu are as follows.

  1. Vertex 11 of the tree is the root. The tree grows upward from the root.
  2. Vertices other than 11 are the points where a branch forks or ends. A branch can be connected to a vertex. However, branches forked from different vertices do not merge at a vertex.
  3. An edge is a branch. Both ends of a branch must be vertices.
  4. A vertex can bear at most one fruit. The root bears no fruit. Before each query, every vertex that can bear fruit does bear fruit.

After building the tree, Geunseong came up with two kinds of queries.

  • 1 i j w: (Grafting) Attach a branch to an arbitrary vertex ii.
    • Vertex ii belongs to the same graph as the root.
    • Since a branch always ends at a vertex, the vertex numbered jj is created at the far end of the new branch.
    • Vertex jj bears a fruit of weight ww. w=0w=0 means no fruit grows there.
    • (1≤i,j≤N+M;1 \le i, j \le N + M; 0≤w≤5000 \le w \le 500)
  • 2 i: (Harvest) Output the force needed to shake vertex ii so that every fruit on vertex ii and on all vertices connected above it falls.
    • Shaking vertex ii shakes every branch and vertex connected above it with the same force.
    • To knock down all the fruit, the force must equal the sum of the fruit weights on vertex ii and on all vertices connected above it.
    • Shaking with force 00 is not allowed, so if the total weight is 00, output -1.
    • (1≤i≤N+M1 \le i \le N + M)

The input values are all integers, and the input is guaranteed to be valid. There is at least one harvest query.

Geunseong left for the clubroom in the middle of writing these queries. Please solve them for him.

Input

The first line contains the initial number of vertices NN and the number of queries MM, separated by a space. The vertices initially have distinct numbers from 11 to NN. (2≤N,M≤100 0002 \le N, M \le 100\,000)

The second line contains, for each vertex from 11 to NN, the vertex directly above it that it is connected to by a branch, separated by spaces. Vertex 11 is the root, so it is given as -1. Every vertex eventually belongs to the same graph as the root, but some may not yet belong to it during the input.

The third line contains the fruit weight wiw_i of each vertex from 11 to NN, separated by spaces. (0≤wi≤5000 \le w_i \le 500) A value of 00 means no fruit grows there. The root bears no fruit.

The next MM lines each contain one query.

Output

For each harvest query, output the force needed to shake the vertex. If that force is 00, output -1.

Hint

The first sample starts with the initial tree shape. In the first query, shaking vertex 11 also shakes vertices 22 and 33. Vertex 33 holds a fruit that needs a force of 33 to knock down, so the force is 33.

This picture shows the tree after three grafts. Shaking vertex 44 shakes only vertex 44, and knocking down its fruit requires a force of 55. Likewise, shaking vertex 77 shakes vertices 77, 44, and 55. Knocking down the fruit on vertices 44 and 77 requires a force of 99.

In the second sample, the first query shakes vertex 11, which also shakes vertices 22, 33, and 44. None of them bears a fruit, so the force is 00 and the output is -1.

Shaking vertex 11 shakes vertices 22, 33, 44, and 77. Knocking down the fruit on vertex 77 requires a force of 500500.

Examples2

  1. Example 1

    Input
    3 7
    -1 1 1
    0 0 3
    2 1
    1 2 7 4
    1 7 4 5
    1 7 5 0
    2 4
    2 7
    2 1
    
    Expected output
    3
    5
    9
    12
    
  2. Example 2

    Input
    4 3
    -1 1 1 1
    0 0 0 0
    2 1
    1 2 7 500
    2 1
    
    Expected output
    -1
    500