#include <bits/stdc++.h>
using namespace std;
const int maxN = 300 * 1000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
int a[maxN], pos[maxN];
int fen[maxN];
void add(int n, int k) {
  for (; n < maxN; n += n & (-n)) fen[n] += k;
}
int get(int n) {
  int res = 0;
  for (; n > 0; n -= n & (-n)) res += fen[n];
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], pos[a[i]] = i;
  for (int i = 1; i <= n; i++)
    if (pos[i] < pos[i - 1]) add(i, 1);
  int q;
  cin >> q;
  while (q--) {
    int t, x, y;
    cin >> t >> x >> y;
    if (t == 1) {
      int tmp = 0;
      if (pos[x] >= pos[x - 1]) tmp = 1;
      cout << get(y) - get(x - 1) + tmp << endl;
      continue;
    }
    x--;
    y--;
    if (pos[a[x]] < pos[a[x] - 1]) add(a[x], -1);
    if (a[x] < n && pos[a[x]] > pos[a[x] + 1]) add(a[x] + 1, -1);
    if (a[x] != a[y] - 1 && pos[a[y]] < pos[a[y] - 1]) add(a[y], -1);
    if (a[y] < n && a[x] != a[y] + 1 && pos[a[y]] > pos[a[y] + 1])
      add(a[y] + 1, -1);
    swap(a[x], a[y]);
    swap(pos[a[x]], pos[a[y]]);
    if (pos[a[x]] < pos[a[x] - 1]) add(a[x], 1);
    if (a[x] < n && pos[a[x]] > pos[a[x] + 1]) add(a[x] + 1, 1);
    if (a[x] != a[y] - 1 && pos[a[y]] < pos[a[y] - 1]) add(a[y], 1);
    if (a[y] < n && a[x] != a[y] + 1 && pos[a[y]] > pos[a[y] + 1])
      add(a[y] + 1, 1);
  }
  return 0;
}
