func_code_string: #include <bits/stdc++.h>   using namespace std;   long n, ar[500000], ps[500000], q, tp, a, b, bd[500000], sb[500000];   long res, fb;   long gs(long v) {     fb = v / 200;     res = 0;     for (int i = 0; i < fb; i++) res += sb[i];     for (int i = fb * 200; i <= v; i++) res += bd[i];     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ar[i];       ps[ar[i]] = i;     }     for (int i = 2; i <= n; i++)       if (ps[i] < ps[i - 1]) {         bd[i] = 1;         sb[i / 200]++;       }     cin >> q;     for (; q; --q) {       cin >> tp;       if (tp == 1) {         cin >> a >> b;         cout << gs(b) - gs(a) + 1 << endl;       } else {         cin >> a >> b;         swap(ar[a], ar[b]);         ps[ar[a]] = a;         ps[ar[b]] = b;         a = ar[a];         b = ar[b];         sb[a / 200] -= bd[a];         sb[b / 200] -= bd[b];         bd[a] = 0;         bd[b] = 0;         sb[(a + 1) / 200] -= bd[a + 1];         sb[(b + 1) / 200] -= bd[b + 1];         bd[a] = bd[b] = bd[a + 1] = bd[b + 1] = 0;         if (a > 1 && ps[a] < ps[a - 1]) {           bd[a] = 1;           sb[a / 200]++;         }         if (a < n && ps[a] > ps[a + 1]) {           bd[a + 1] = 1;           sb[(a + 1) / 200]++;         }         if (b > 1 && ps[b] < ps[b - 1]) {           sb[b / 200] += 1 - bd[b];           bd[b] = 1;         }         if (b < n && ps[b] > ps[b + 1]) {           sb[(b + 1) / 200] += 1 - bd[b + 1];           bd[b + 1] = 1;         }       }     }     cin.get();     cin.get();     return 0;   }   