func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 50;   int n, m, a[N], b[N];   vector<int> G[N];   int in[N], out[N], tim, dep[N];   int mn[20][N], lg[N], p[N], k;   void dfs(int u, int f) {     dep[u] = dep[f] + 1;     in[u] = ++tim;     mn[0][p[u] = ++k] = u;     for (int v : G[u])       if (v != f) dfs(v, u), mn[0][++k] = u;     out[u] = tim;   }   int chk(int x, int y) { return dep[x] < dep[y] ? x : y; }   int LCA(int x, int y) {     x = p[x], y = p[y];     if (x > y) swap(x, y);     int z = lg[y - x + 1];     return chk(mn[z][x], mn[z][y - (1 << z) + 1]);   }   int D(int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }   struct lll {     int x, y, o;     lll(int _x = 0, int _y = 0, int _o = 0) { x = _x, y = _y, o = _o; }     friend lll operator+(const lll &a, const lll &b) {       if (!a.o) return a;       if (!b.o) return b;       if (!a.x) return b;       if (!b.x) return a;       lll t = b;       int A = D(a.x, t.x), B = D(a.x, t.y), C = D(t.x, t.y);       if (A + B == C)         t = lll(t.x, t.y, 1);       else if (B + C == A)         t = lll(a.x, t.x, 1);       else if (A + C == B)         t = lll(a.x, t.y, 1);       else         return lll(0, 0, 0);       A = D(a.y, t.x), B = D(a.y, t.y), C = D(t.x, t.y);       if (A + B == C)         t = lll(t.x, t.y, 1);       else if (B + C == A)         t = lll(a.y, t.x, 1);       else if (A + C == B)         t = lll(a.y, t.y, 1);       else         return lll(0, 0, 0);       return t;     }   };   namespace SGT {   const int N = ::N << 2;   lll t[N];   void up(int x) { t[x] = t[x << 1] + t[x << 1 | 1]; }   void build(int x, int l, int r) {     if (l == r) return t[x] = lll(b[l], b[l], 1), void();     build(x << 1, l, ((l + r) >> 1)), build(x << 1 | 1, ((l + r) >> 1) + 1, r);     up(x);   }   void mdf(int x, int l, int r, int p, int v) {     if (l == r) return t[x] = lll(v, v, 1), void();     if (p <= ((l + r) >> 1))       mdf(x << 1, l, ((l + r) >> 1), p, v);     else       mdf(x << 1 | 1, ((l + r) >> 1) + 1, r, p, v);     up(x);   }   int ask(int x, int l, int r, lll z) {     if (l == r) {       lll y = z + t[x];       if (y.o) return l + 1;       return l;     }     lll y = z + t[x << 1];     if (!y.o) return ask(x << 1, l, ((l + r) >> 1), z);     return ask(x << 1 | 1, ((l + r) >> 1) + 1, r, y);   }   }  // namespace SGT   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), b[a[i]] = i;     for (int i = 2, x; i <= n; i++) scanf( %d , &x), G[x].push_back(i);     dfs(1, 0);     for (int i = 2; i <= k; i++) lg[i] = lg[i >> 1] + 1;     for (int i = 1; (1 << i) <= k; i++)       for (int j = 1; j + (1 << i) - 1 <= k; j++)         mn[i][j] = chk(mn[i - 1][j], mn[i - 1][j + (1 << i - 1)]);     SGT ::build(1, 0, n - 1);     cin >> m;     while (m--) {       int o, x, y;       scanf( %d , &o);       if (o == 1) {         scanf( %d%d , &x, &y);         swap(a[x], a[y]);         x = a[x], y = a[y];         swap(b[x], b[y]);         SGT ::mdf(1, 0, n - 1, x, b[x]);         SGT ::mdf(1, 0, n - 1, y, b[y]);       }       if (o == 2) cout << SGT ::ask(1, 0, n - 1, lll(0, 0, 1)) <<   n ;     }     return 0;   }   