atcoder#ABC295G. [ABC295G] Minimum Reachable City

[ABC295G] Minimum Reachable City

Score : 600600 points

Problem Statement

We have a directed graph GSG_S with NN vertices numbered 11 to NN. It has N1N-1 edges. The ii-th edge (1iN1)(1\leq i \leq N-1) goes from vertex pi (1pii)p_i\ (1\leq p_i \leq i) to vertex i+1i+1.

We have another directed graph GG with NN vertices numbered 11 to NN. Initially, GG equals GSG_S. Process QQ queries on GG in the order they are given. There are two kinds of queries as follows.

  • 1 u v : Add an edge to GG that goes from vertex uu to vertex vv. It is guaranteed that the following conditions are satisfied.- uvu \neq v.
    • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • uvu \neq v.
  • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • 2 x : Print the smallest vertex number of a vertex reachable from vertex xx via some edges on GG (including vertex xx).

Constraints

  • 2N2×1052\leq N \leq 2\times 10^5
  • 1Q2×1051\leq Q \leq 2\times 10^5
  • 1pii1\leq p_i\leq i
  • For each query in the first format:- 1u,vN1\leq u,v \leq N.
    • uvu \neq v.
    • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • 1u,vN1\leq u,v \leq N.
  • uvu \neq v.
  • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • For each query in the second format, 1xN1\leq x \leq N.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

p1p_1 p2p_2 \dots pN1p_{N-1}

QQ

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Here, queryi\mathrm{query}_i denotes the ii-th query and is in one of the following formats:

11 uu vv

22 xx

Output

Print qq lines, where qq is the number of queries in the second format. The ii-th line (1jq)(1\leq j \leq q) should contain the answer to the jj-th query in the second format.

5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4
4
2
1
  • At the time of the first query, only vertex 44 is reachable from vertex 44 via some edges on GG.
  • At the time of the third query, vertices 22, 33, 44, and 55 are reachable from vertex 44 via some edges on GG.
  • At the time of the fifth query, vertices 11, 22, 33, 44, and 55 are reachable from vertex 44 via some edges on GG.
7
1 1 2 2 3 3
10
2 5
1 5 2
2 5
1 2 1
1 7 1
1 6 3
2 5
2 6
2 1
1 7 1
5
2
1
1
1