//84104971101048411497 - Can you guess what does this mean?
using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define ftoa(i, l, r, a) for(int i = (l); i <= (r); i += a)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define maxN 100005
#define maxB 20
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
struct FenwickTree {
vector<ll> t;
int n;
void init(int _n) {
n = _n;
t.resize(n+1);
}
int update(int p, int x) {
for(int i = p; i <= n; i += i &(-i)) {
t[i] += x;
}
}
int query(int p) {
ll ans = 0;
for(int i = p; i >= 1; i -= i &(-i)) {
ans += t[i];
}
return ans;
}
void print() {
fto(i, 1, n) printf("%d ", t[i]);
puts("");
}
};
int n, k, q, p[maxN], l[maxN], r[maxN], d[maxN], b[maxB][maxN], cnt;
vector<int> ke[maxN];
FenwickTree T;
int LCA(int u, int v) {
if (d[u] < d[v]) swap(u, v);
int t = d[u]-d[v];
fto(i, 0, k) {
if (t&(1<<i)) u = b[i][u];
}
fdto(i, k, 0) {
if (b[i][u] != b[i][v]) {
u = b[i][u];
v = b[i][v];
}
}
if (u == v) return u;
fto(i, 0, k) {
if (b[i][u] == b[i][v]) return b[i][u];
}
}
void DFS(int u) {
l[u] = ++cnt;
forit(it, ke[u]) {
int v = *it;
if (v != p[u]) {
d[v] = d[u]+1;
DFS(v);
}
}
r[u] = cnt;
}
int main () {
scanf("%d", &n);
fto(i, 2, n) {
scanf("%d", &p[i]);
ke[i].pb(p[i]);
ke[p[i]].pb(i);
}
DFS(1);
int k = (int)log2(n);
fto(u, 1, n) b[0][u] = p[u];
fto(i, 1, k) {
fto(u, 1, n) b[i][u] = b[i-1][b[i-1][u]];
}
T.init(n);
fto(i, 1, n) {
T.update(l[i], d[i]);
T.update(l[i]+1, -d[i]);
}
scanf("%d", &q);
fto(i, 1, q) {
int t, u, v;
scanf("%d", &t);
if (t == 1) {
scanf("%d%d", &u, &v);
int a = LCA(u, v);
printf("%d\n", T.query(l[u])+T.query(l[v])-2*T.query(l[a]));
}
else {
scanf("%d", &u);
T.update(l[u], -1);
T.update(r[u]+1, 1);
}
}
return 0;
}
Ly84NDEwNDk3MTEwMTA0ODQxMTQ5NyAtIENhbiB5b3UgZ3Vlc3Mgd2hhdCBkb2VzIHRoaXMgbWVhbj8KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbWFwaWkgbWFwPGludCwgaW50PgojZGVmaW5lIGRlYnVnKGEpIGNvdXQgPDwgI2EgPDwgIjogIiA8PCBhIDw8IGVuZGwKI2RlZmluZSBkZWJ1Z2ExKGEsIGwsIHIpIGZ0byhpLCBsLCByKSBjb3V0IDw8IGFbaV0gPDwgIiAiOyBjb3V0IDw8IGVuZGwKI2RlZmluZSBmZHRvKGksICByLCBsKSBmb3IoaW50IGkgPSAocik7IGkgPj0gKGwpOyAtLWkpCiNkZWZpbmUgZnRvKGksIGwsIHIpIGZvcihpbnQgaSA9IChsKTsgaSA8PSAocik7ICsraSkKI2RlZmluZSBmdG9hKGksIGwsIHIsIGEpIGZvcihpbnQgaSA9IChsKTsgaSA8PSAocik7IGkgKz0gYSkKI2RlZmluZSBmb3JpdChpdCwgdmFyKSBmb3IoX190eXBlb2YodmFyLmJlZ2luKCkpIGl0ID0gdmFyLmJlZ2luKCk7IGl0ICE9IHZhci5lbmQoKTsgaXQrKykKI2RlZmluZSBmb3JyaXQocml0LCB2YXI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IHByaW50ZigiJWQgIiwgdFtpXSk7CiAgICAgICAgcHV0cygiIik7CiAgICB9Cn07CgppbnQgbiwgaywgcSwgcFttYXhOXSwgbFttYXhOXSwgclttYXhOXSwgZFttYXhOXSwgYlttYXhCXVttYXhOXSwgY250Owp2ZWN0b3I8aW50PiBrZVttYXhOXTsKRmVud2lja1RyZWUgVDsKCmludCBMQ0EoaW50IHUsIGludCB2KSB7CiAgICBpZiAoZFt1XSA8IGRbdl0pIHN3YXAodSwgdik7CiAgICBpbnQgdCA9IGRbdV0tZFt2XTsKICAgIGZ0byhpLCAwLCBrKSB7CiAgICAgICAgaWYgKHQmKDE8PGkpKSB1ID0gYltpXVt1XTsKICAgIH0KICAgIGZkdG8oaSwgaywgMCkgewogICAgICAgIGlmIChiW2ldW3VdICE9IGJbaV1bdl0pIHsKICAgICAgICAgICAgdSA9IGJbaV1bdV07CiAgICAgICAgICAgIHYgPSBiW2ldW3ZdOwogICAgICAgIH0KICAgIH0KICAgIGlmICh1ID09IHYpIHJldHVybiB1OwogICAgZnRvKGksIDAsIGspIHsKICAgICAgICBpZiAoYltpXVt1XSA9PSBiW2ldW3ZdKSByZXR1cm4gYltpXVt1XTsKICAgIH0KfQoKdm9pZCBERlMoaW50IHUpIHsKICAgIGxbdV0gPSArK2NudDsKICAgIGZvcml0KGl0LCBrZVt1XSkgewogICAgICAgIGludCB2ID0gKml0OwogICAgICAgIGlmICh2ICE9IHBbdV0pIHsKICAgICAgICAgICAgZFt2XSA9IGRbdV0rMTsKICAgICAgICAgICAgREZTKHYpOwogICAgICAgIH0KICAgIH0KICAgIHJbdV0gPSBjbnQ7Cn0KCmludCBtYWluICgpIHsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGZ0byhpLCAyLCBuKSB7CiAgICAgICAgc2NhbmYoIiVkIiwgJnBbaV0pOwogICAgICAgIGtlW2ldLnBiKHBbaV0pOwogICAgICAgIGtlW3BbaV1dLnBiKGkpOwogICAgfQoKICAgIERGUygxKTsKICAgIGludCBrID0gKGludClsb2cyKG4pOwogICAgZnRvKHUsIDEsIG4pIGJbMF1bdV0gPSBwW3VdOwogICAgZnRvKGksIDEsIGspIHsKICAgICAgICBmdG8odSwgMSwgbikgYltpXVt1XSA9IGJbaS0xXVtiW2ktMV1bdV1dOwogICAgfQoKICAgIFQuaW5pdChuKTsKICAgIGZ0byhpLCAxLCBuKSB7CiAgICAgICAgVC51cGRhdGUobFtpXSwgZFtpXSk7CiAgICAgICAgVC51cGRhdGUobFtpXSsxLCAtZFtpXSk7CiAgICB9CgogICAgc2NhbmYoIiVkIiwgJnEpOwogICAgZnRvKGksIDEsIHEpIHsKICAgICAgICBpbnQgdCwgdSwgdjsKICAgICAgICBzY2FuZigiJWQiLCAmdCk7CiAgICAgICAgaWYgKHQgPT0gMSkgewogICAgICAgICAgICBzY2FuZigiJWQlZCIsICZ1LCAmdik7CiAgICAgICAgICAgIGludCBhID0gTENBKHUsIHYpOwogICAgICAgICAgICBwcmludGYoIiVkXG4iLCBULnF1ZXJ5KGxbdV0pK1QucXVlcnkobFt2XSktMipULnF1ZXJ5KGxbYV0pKTsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsICZ1KTsKICAgICAgICAgICAgVC51cGRhdGUobFt1XSwgLTEpOwogICAgICAgICAgICBULnVwZGF0ZShyW3VdKzEsIDEpOwogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gMDsKfQo=