The Tree
Contest: Codeforces Round 502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)
Difficulty rating: 3200
Tags: data structures

Abendsen assigned a mission to Juliana. In this mission, Juliana has a rooted tree with $$$n$$$ vertices. Vertex number $$$1$$$ is the root of this tree. Each vertex can be either black or white. At first, all vertices are white. Juliana is asked to process $$$q$$$ queries. Each query is one of three types:

1. If vertex $$$v$$$ is white, mark it as black; otherwise, perform this operation on all direct sons of $$$v$$$ instead.
2. Mark all vertices in the subtree of $$$v$$$ (including $$$v$$$) as white.
3. Find the color of the $$$i$$$-th vertex.

An example of operation "1 1" (corresponds to the first example test). The vertices $$$1$$$ and $$$2$$$ are already black, so the operation goes to their sons instead.

Can you help Juliana to process all these queries?

Input format:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2\leq n\leq 10^5$$$, $$$1\leq q\leq 10^5$$$) — the number of vertices and the number of queries.

The second line contains $$$n-1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1\leq p_i<i$$$), where $$$p_i$$$ means that there is an edge between vertices $$$i$$$ and $$$p_i$$$.

Each of the next $$$q$$$ lines contains two integers $$$t_i$$$ and $$$v_i$$$ ($$$1\leq t_i\leq 3$$$, $$$1\leq v_i\leq n$$$) — the type of the $$$i$$$-th query and the vertex of the $$$i$$$-th query.

It is guaranteed that the given graph is a tree.

Output format:
For each query of type $$$3$$$, print "black" if the vertex is black; otherwise, print "white".

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
The first example is shown on the picture below.

The second example is shown on the picture below.

Example input:
8 10
1 2 1 2 5 4 5
1 2
3 2
3 1
1 1
1 1
3 5
3 7
3 4
2 2
3 5

Example output:
black
white
black
white
black
white

Example input:
8 11
1 1 2 3 3 6 6
1 1
1 1
1 3
3 2
3 4
3 6
3 7
2 3
1 6
3 7
3 6

Example output:
black
white
black
white
white
black

Editorial:
The problem can be solved using HLD or sqrt-decomposition on queries. Here, I will explain to you the second solution.
Let $$$s$$$ be a constant.
Let's split all queries on $$$\frac{n}{s}$$$ blocks. Each block will contain $$$s$$$ queries.
In each block, since we have $$$s$$$ queries, we will have at most $$$s$$$ different vertices there.   Therefore, we can squeeze the tree using only those $$$s$$$ different vertices. There will be a directed edge from $$$i$$$ to $$$j$$$ if they are both in that set of $$$s$$$ different vertices and $$$i$$$ is an ancestor of $$$j$$$ in the original tree. A weight of such edge will be the number of white vertices on the way between those two vertices (exclusively $$$i$$$ and $$$j$$$).
If we want to find answers for the queries in the black, we need to make every operation in $$$\mathcal{O}(s)$$$. In each vertex, we will have $$$p_i$$$ — the number of operations of the first type that need to be processed on that vertex. Also, each vertex will have $$$c_i$$$ — the boolean variable that means that we need to clear the subtree. Obviously, when we are doing the second operation, we need to clear every $$$p_i$$$ in that subtree.
After we answer the queries of the $$$i$$$-th block, we need to update the full graph.
So, the complexity will be $$$\mathcal{O}(\frac{n}{s}\cdot n + n\cdot s)$$$. If you make $$$s = \sqrt{n}$$$, the complexity will be $$$\mathcal{O}(n\cdot \sqrt{n})$$$.
For better understanding, you can check the code below.