#P8844. Yellow Nodes in Subtree

    ID: 22008 Type: Default 1000ms 256MiB

Yellow Nodes in Subtree

Yellow Nodes in Subtree

You are given a rooted tree with \( n (1\le n\le 10^5) \) nodes. The root is node \(1\) with depth \(1\). Initially, all nodes are colored green.

There are \( m (1\le m\le 10^5) \) operations. Two types of operations are supported:

  • Operation 1: Reset the entire tree to green, and then color all nodes with depth \(\ge x\) yellow. Formally, for a given integer \( x \), the color of a node with depth \( d \) becomes yellow if \( d\ge x \) and remains green otherwise.
  • Operation 2: For a given node \( x \), report the number of yellow nodes in the subtree of \( x \) (including \( x \)).

Note: The operations are processed sequentially. Initially, since all nodes are green, a query will return \(0\) yellow nodes.

inputFormat

The first line contains two integers \( n \) and \( m \): the number of nodes and the number of operations.

The second line contains \( n-1 \) integers: \( p_2, p_3, \ldots, p_n \), where \( p_i \) denotes the parent of node \( i \) (\( 2\le i\le n \)).

Each of the next \( m \) lines describes an operation in one of the following formats:

  • "1 x" — Operation 1: Reset the tree to green then color nodes with depth \(\ge x\) yellow.
  • "2 x" — Operation 2: Query the number of yellow nodes in the subtree of node \( x \).

outputFormat

For each operation of type 2, output a single integer representing the number of yellow nodes in the subtree of the specified node.

sample

3 3
1 1
2 1
1 2
2 1
0

2

</p>