func_code_string: #include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 29;   const double EPS = 1e-8;   const double PI = acos(-1);   const int MAX_N = 400000;   struct SegSum {     int bit[MAX_N + 1];     int n;     void init(int _n) {       n = _n;       memset(bit, 0, sizeof(bit));     }     int sum(int i) {       int s = 0;       while (i > 0) {         s += bit[i];         i -= i & -i;       }       return s;     }     int sum(int a, int b) { return sum(b) - sum(a - 1); }     void add(int i, int x) {       while (i <= n) {         bit[i] += x;         i += i & -i;       }     }   } SegSum;   int a[300000];   int pos[300000];   int main() {     int n;     while (cin >> n) {       for (int i = 0; i < (int)n; ++i) {         scanf( %d , a + i);         a[i]--;         pos[a[i]] = i;       }       SegSum.init(n);       for (int i = 0; i < (int)n; ++i) {         if (a[i] == 0 || pos[a[i] - 1] > i) {           SegSum.add(a[i] + 1, 1);         }       }       int q;       cin >> q;       for (int i = 0; i < (int)q; ++i) {         int k, x, y;         scanf( %d%d%d , &k, &x, &y);         x--;         y--;         if (k == 1) {           int r = SegSum.sum(x + 1, y + 1);           if (SegSum.sum(x + 1, x + 1) == 0) r++;           cout << r << endl;         } else {           if (a[x] + 1 < n && pos[a[x] + 1] > x && pos[a[x] + 1] < y) {             SegSum.add(a[x] + 1 + 1, 1);           }           if (a[x] - 1 >= 0 && pos[a[x] - 1] > x && pos[a[x] - 1] < y) {             SegSum.add(a[x] + 1, -1);           }           if (a[y] + 1 < n && pos[a[y] + 1] >= x && pos[a[y] + 1] < y) {             SegSum.add(a[y] + 1 + 1, -1);           }           if (a[y] - 1 >= 0 && pos[a[y] - 1] >= x && pos[a[y] - 1] < y) {             SegSum.add(a[y] + 1, 1);           }           swap(a[x], a[y]);           swap(pos[a[x]], pos[a[y]]);         }       }     }   }   