#K80992. Tree Updates and Subtree Maximum Query

    ID: 35653 Type: Default 1000ms 256MiB

Tree Updates and Subtree Maximum Query

Tree Updates and Subtree Maximum Query

You are given a rooted tree with (n) nodes numbered from 1 to (n). The tree is represented by an array of (n-1) integers where the (i)-th integer indicates the parent of node (i+1) (node 1 is the root). Every node initially has a value of 0. You will be given a series of operations of two types:

  1. Update operation: "1 v x" which sets the value of node (v) to (x).
  2. Query operation: "2 v" which asks for the maximum value in the subtree rooted at node (v).

Your task is to process these operations and output the result of each query operation. The operations are provided via standard input and the output should be printed to standard output.

Note: The subtree of a node includes the node itself and all its descendants.

inputFormat

The input is given via standard input and consists of multiple lines.

The first line contains an integer (n) representing the number of nodes in the tree.

The second line contains (n-1) space-separated integers where the (i)-th integer is the parent of node (i+1). If (n=1), this line will be empty.

The third line contains an integer (q) representing the number of operations.

Each of the following (q) lines represents an operation in one of the following forms:

  • "1 v x" : update the value of node (v) to (x).
  • "2 v" : query the maximum value in the subtree rooted at node (v).

outputFormat

For each query operation (i.e. lines starting with "2"), output the maximum value found in the subtree rooted at the given node. Each result should be printed on a separate line to standard output.## sample

5
1 1 2 2
6
1 1 5
1 3 3
1 4 7
2 1
2 2
2 5
7

7 0

</p>