Under construction

Still being built. Things may change

Tree

Time limit2sMemory limit512 MB

Summary
Maintain a rooted tree under vertex deletions (children reparent to grandparent) and answer distance queries between two live vertices.
Level

Hard8 of 10

Topics
Tree, DFS, Segment tree, Dynamic programming
Solved
No attempts yet

Problem

You are given a rooted tree with nn vertices. The vertices are numbered from 1 to nn, and vertex 1 is the root. A rooted tree is a connected undirected graph with one vertex chosen as the root. For every vertex, the neighbor closest to the root is its parent, and the other neighbors are its children. The root has no parent.

Two kinds of queries are processed in the given order.

The first kind removes a vertex. When vertex uu is removed, every child of uu becomes a child of the parent of uu. The root is never removed.

The second kind asks for a distance. It is the length of the path between two vertices in the current tree, counted as the number of edges on that path.

Answer every distance query.

Input

The first line contains the number of vertices nn in the initial tree (1≤n≤1000001 \le n \le 100000).

The second line contains the parents pip_i of vertices 2,3,…,n2, 3, \ldots, n in this order (1≤pi≤n1 \le p_i \le n). When nn is 1, the second line is empty.

The third line contains the number of queries qq (1≤q≤1000001 \le q \le 100000).

Each of the next qq lines holds one query. A query starts with an integer that is either 1 or 2. If that integer is 1, two integers aa and bb follow (1≤a,b≤n1 \le a, b \le n), and you must report the distance between aa and bb in the current tree. If it is 2, one integer vv follows (1≤v≤n1 \le v \le n), and vertex vv must be removed.

The initial tree is always valid. Every vertex named in a query is still present, and the root is never given as a vertex to remove.

Output

For each query that starts with 1, print the distance between the two vertices on its own line.

Examples3

  1. Example 1

    Input
    8
    1 5 2 1 2 5 5
    7
    2 4
    1 7 6
    2 5
    1 3 8
    1 8 6
    2 2
    1 8 6
    
    Expected output
    4
    2
    3
    2
    
  2. Example 2

    Input
    1
    
    1
    1 1 1
    
    Expected output
    0
    
  3. Example 3

    Input
    6
    1 2 3 4 5
    10
    1 1 6
    2 3
    1 1 6
    2 4
    1 1 6
    1 2 6
    2 5
    1 2 6
    2 2
    1 1 6
    
    Expected output
    5
    4
    3
    2
    1
    1