func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   const int K = 20;   int go[N][K];   int tin[N], tout[N];   vector<int> g[N];   int timer = 0;   int perm[N];   int where[N];   pair<int, int> t[4 * N];   int n;   void dfs(int u, int p = 0) {     go[u][0] = p;     tin[u] = timer++;     for (int i = 1; i < K; ++i) {       go[u][i] = go[go[u][i - 1]][i - 1];     }     for (auto v : g[u]) {       if (v != p) {         dfs(v, u);       }     }     tout[u] = timer++;   }   bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }   int lca(int a, int b) {     if (anc(a, b)) return a;     for (int k = K - 1; k >= 0; --k) {       if (!anc(go[a][k], b)) {         a = go[a][k];       }     }     return go[a][0];   }   pair<int, int> null = {-1, -1};   pair<int, int> empt = {-2, -2};   int d;   bool on_vert(int top, int bot, int v) { return anc(top, v) && anc(v, bot); }   bool check(pair<int, int> p, pair<int, int> a) {     int lc = lca(p.first, p.second);     if (!on_vert(lc, p.first, a.first) && !on_vert(lc, p.second, a.first))       return false;     if (!on_vert(lc, p.first, a.second) && !on_vert(lc, p.second, a.second))       return false;     return true;   }   pair<int, int> merge(pair<int, int> a, pair<int, int> b) {     if (a.first == -1 || b.first == -1) return {-1, -1};     if (check({a.first, b.first}, a) && check({a.first, b.first}, b))       return {a.first, b.first};     if (check({a.first, b.second}, a) && check({a.first, b.second}, b))       return {a.first, b.second};     if (check({a.second, b.first}, a) && check({a.second, b.first}, b))       return {a.second, b.first};     if (check({a.second, b.second}, a) && check({a.second, b.second}, b))       return {a.second, b.second};     if (check({a.first, a.second}, a) && check({a.first, a.second}, b))       return {a.first, a.second};     if (check({b.first, b.second}, a) && check({b.first, b.second}, b))       return {b.first, b.second};     return {-1, -1};   }   void build(int l, int r, int v) {     if (l == r) {       t[v] = {where[l], where[l]};     } else {       int m = (l + r) >> 1;       build(l, m, 2 * v + 1);       build(m + 1, r, 2 * v + 2);       t[v] = merge(t[2 * v + 1], t[2 * v + 2]);     }   }   void modify(int ind, int l, int r, int v) {     if (l == r) {       t[v] = {where[ind], where[ind]};     } else {       int m = (l + r) >> 1;       if (ind <= m)         modify(ind, l, m, 2 * v + 1);       else         modify(ind, m + 1, r, 2 * v + 2);       t[v] = merge(t[2 * v + 1], t[2 * v + 2]);     }   }   void update(int a, int b) {     int pa = perm[a];     int pb = perm[b];     swap(where[perm[a]], where[perm[b]]);     swap(perm[a], perm[b]);     modify(pa, 0, n - 1, 0);     modify(pb, 0, n - 1, 0);   }   int get(int v, int l, int r, pair<int, int> cur) {     if (l == r) {       return l;     }     int m = (l + r) >> 1;     auto mem = merge(t[2 * v + 1], cur);     if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem);     return get(2 * v + 1, l, m, cur);   }   int mex() {     if (t[0].first != -1) return n;     return get(0, 0, n - 1, {where[0], where[0]});   }   void init() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> perm[i];       where[perm[i]] = i;     }     for (int i = 1; i < n; ++i) {       int p;       cin >> p;       --p;       g[p].push_back(i);     }     dfs(0);     build(0, n - 1, 0);   }   void solve() {     int q;     cin >> q;     while (q--) {       int t;       cin >> t;       if (t == 1) {         int a, b;         cin >> a >> b;         --a, --b;         update(a, b);       } else {         cout << mex() <<   n ;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(20);     init();     solve();   }   