#include <bits/stdc++.h>   using namespace std;   int aaa = 0;   int n, pown = 1, a[250005], indx, indy, p[250005], lvl[250005], timer,          in[250005], out[250005], pw[4 * 250005], st, en, lc, oo, c[6], ans, q,          typ;   vector<int> v[250005], gg;   pair<int, pair<int, int> > tt[2 * 250005], l[2 * 250005][19], r[2 * 250005][19],       t[4 * 250005];   inline void dfs(int x, int par) {     gg.push_back(x);     p[x] = par;     timer++;     in[x] = timer;     for (int i = 0; i < v[x].size(); i++) {       if (v[x][i] == par) continue;       lvl[v[x][i]] = lvl[x] + 1;       dfs(v[x][i], x);     }     gg.push_back(x);     timer++;     out[x] = timer;   }   inline int lca(int x, int y) {     if (lvl[x] > lvl[y]) swap(x, y);     if (in[x] <= in[y] && out[x] >= out[y]) return x;     int t1 = in[x];     if (t1 > in[y]) t1 = in[y];     int t2 = out[x];     if (t2 < out[y]) t2 = out[y];     int sz = pw[t2 - t1];     pair<int, pair<int, int> > ret = l[t1][sz];     if (ret > r[t2][sz]) ret = r[t2][sz];     return p[gg[ret.second.first - 1]];   }   pair<int, pair<int, int> > doit() {     for (st = 1; st <= 4; st++) {       for (en = st + 1; en <= 4; en++) {         lc = lca(c[st], c[en]);         oo = 1;         for (int i = 1; i <= 4; i++) {           if (!((in[c[i]] <= in[c[st]] && out[c[st]] <= out[c[i]]) ||                 (in[c[i]] <= in[c[en]] && out[c[en]] <= out[c[i]]))) {             oo = 0;             break;           }           if (!(in[lc] <= in[c[i]] && out[c[i]] <= out[lc])) {             oo = 0;             break;           }         }         if (oo) {           break;         }       }       if (oo) break;     }     if (oo)       return make_pair(1, make_pair(c[st], c[en]));     else       return make_pair(0, make_pair(0, 0));   }   void upd(int x) {     if (!x) return;     t[x].first = 0;     if (t[2 * x].first && t[2 * x + 1].first) {       c[1] = t[2 * x].second.first;       c[2] = t[2 * x].second.second;       c[3] = t[2 * x + 1].second.first;       c[4] = t[2 * x + 1].second.second;       t[x] = doit();     }     upd(x / 2);   }   void cnt(int x, int L, int R) {     if (t[x].first) {       if (!c[1]) {         c[1] = t[x].second.first;         c[2] = t[x].second.second;         ans += R - L + 1;         return;       }       c[3] = t[x].second.first;       c[4] = t[x].second.second;       pair<int, pair<int, int> > ret = doit();       if (ret.first) {         c[1] = ret.second.first;         c[2] = ret.second.second;         ans += R - L + 1;         return;       }     }     if (!t[2 * x].first) {       if (L == R) return;       cnt(2 * x, L, (L + R) / 2);     } else {       if (!c[1]) {         c[1] = t[2 * x].second.first;         c[2] = t[2 * x].second.second;         ans += (L + R) / 2 - L + 1;         if (L == R) return;         cnt(2 * x + 1, (L + R) / 2 + 1, R);       } else {         c[3] = t[2 * x].second.first;         c[4] = t[2 * x].second.second;         pair<int, pair<int, int> > ret = doit();         if (ret.first) {           c[1] = ret.second.first;           c[2] = ret.second.second;           ans += (L + R) / 2 - L + 1;           if (L == R) return;           cnt(2 * x + 1, (L + R) / 2 + 1, R);         } else {           if (L == R) return;           cnt(2 * x, L, (L + R) / 2);         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     while (pown <= n) {       pown *= 2;     }     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i]++;     }     for (int i = 2; i <= n; i++) {       int x;       cin >> x;       v[x].push_back(i);     }     cin >> q;     dfs(1, 1);     for (int i = 0; i < gg.size(); i++) {       tt[i + 1] = make_pair(lvl[gg[i]], make_pair(i + 1, i + 1));     }     for (int i = 2 * n; i >= 1; i--) {       l[i][0] = tt[i];       if (tt[i + 1].first < tt[i].first && i != 2 * n) l[i][0] = tt[i + 1];       if (i != 2 * n) l[i][0].second.second = i + 1;       r[l[i][0].second.second][0] = l[i][0];       for (int j = 1; j <= 18; j++) {         l[i][j] = l[i][j - 1];         if (l[i][j].first > l[l[i][j - 1].second.second][j - 1].first) {           l[i][j].first = l[l[i][j - 1].second.second][j - 1].first;           l[i][j].second.first = l[l[i][j - 1].second.second][j - 1].second.first;         }         l[i][j].second.second = l[l[i][j - 1].second.second][j - 1].second.second;         r[l[i][j].second.second][j] = l[i][j];       }     }     pw[1] = 0;     int pp = 2, txx = 1;     for (int i = 1; i < 19; i++) {       for (int j = pp; j < 2 * pp; j++) {         pw[j] = txx;       }       pp *= 2;       txx++;     }     for (int i = 1; i <= n; i++) {       t[pown + a[i] - 1].first = 1;       t[pown + a[i] - 1].second.first = i;       t[pown + a[i] - 1].second.second = i;       upd((pown + a[i] - 1) / 2);     }     while (q--) {       cin >> typ;       if (typ == 1) {         cin >> indx >> indy;         t[pown + a[indx] - 1].first = 1;         t[pown + a[indx] - 1].second.first = indy;         t[pown + a[indx] - 1].second.second = indy;         upd((pown + a[indx] - 1) / 2);         t[pown + a[indy] - 1].first = 1;         t[pown + a[indy] - 1].second.first = indx;         t[pown + a[indy] - 1].second.second = indx;         upd((pown + a[indy] - 1) / 2);         swap(a[indx], a[indy]);       } else {         ans = 0;         for (int i = 1; i <= 4; i++) c[i] = 0;         cnt(1, 1, pown);         cout << ans << endl;       }     }     return 0;   }