#include <bits/stdc++.h>
using namespace std;
int n, q, op;
set<pair<int, int> > s;
int x, y;
int bit[300010];
int v[300010];
void update(int x, int v) {
  while (x < 300010) {
    bit[x] += v;
    x += (x & -x);
  }
}
int query(int x) {
  int sum = 0;
  while (x > 0) {
    sum += bit[x];
    x -= (x & -x);
  }
  return sum;
}
void printset() {
  printf("Set -------- \n");
  for (auto it = s.begin(); it != s.end(); ++it) {
    printf("%d - %d\n", it->first, it->second);
  }
}
int main(void) {
  ios ::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    s.insert(make_pair(v[i], i));
  }
  for (auto it = s.begin(); it != s.end(); ++it) {
    if (it->first == 1) {
      update(it->first, 1);
    } else {
      auto it2 = s.lower_bound(make_pair(it->first - 1, -1));
      if (it2->second > it->second) {
        update(it->first, 1);
      }
    }
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> op >> x >> y;
    if (op == 1) {
      int ans = query(y) - query(x - 1);
      if (query(x) - query(x - 1) == 0) ans++;
      cout << ans << "\n";
    } else {
      {
        s.erase(make_pair(v[x], x));
        s.insert(make_pair(v[x], y));
        if (v[x] > 1) {
          auto it = s.lower_bound(make_pair(v[x] - 1, -1));
          if (it->second > y) {
            if (query(v[x]) - query(v[x] - 1) == 0) update(v[x], 1);
          } else {
            if (query(v[x]) - query(v[x] - 1) == 1) update(v[x], -1);
          }
        }
      }
      {
        if (v[x] < n) {
          auto it = s.lower_bound(make_pair(v[x] + 1, -1));
          if (it->second < y) {
            if (query(v[x] + 1) - query(v[x]) == 0) update(v[x] + 1, 1);
          } else {
            if (query(v[x] + 1) - query(v[x]) == 1) update(v[x] + 1, -1);
          }
        }
      }
      {
        s.erase(make_pair(v[y], y));
        s.insert(make_pair(v[y], x));
        if (v[y] > 1) {
          auto it = s.lower_bound(make_pair(v[y] - 1, -1));
          if (it->second > x) {
            if (query(v[y]) - query(v[y] - 1) == 0) update(v[y], 1);
          } else {
            if (query(v[y]) - query(v[y] - 1) == 1) update(v[y], -1);
          }
        }
      }
      {
        if (v[y] < n) {
          auto it = s.lower_bound(make_pair(v[y] + 1, -1));
          if (it->second < x) {
            if (query(v[y] + 1) - query(v[y]) == 0) update(v[y] + 1, 1);
          } else {
            if (query(v[y] + 1) - query(v[y]) == 1) update(v[y] + 1, -1);
          }
        }
      }
      swap(v[x], v[y]);
    }
  }
  return 0;
}
