#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n, a[maxn];
int pos[maxn];
int bit[maxn];
inline int lowbit(int x) { return x & -x; }
void updata(int st, int v) {
  for (int i = st; i <= n; i += lowbit(i)) bit[i] += v;
}
int get_sum(int st) {
  int res = 0;
  for (int i = st; i; i -= lowbit(i)) res += bit[i];
  return res;
}
int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    pos[a[i]] = i;
  }
  for (i = 2; i <= n; ++i)
    if (pos[i] < pos[i - 1]) updata(i, 1);
  int q;
  scanf("%d", &q);
  while (q--) {
    int ope, x, y;
    scanf("%d%d%d", &ope, &x, &y);
    if (ope == 1)
      printf("%d\n", get_sum(y) - get_sum(x) + 1);
    else {
      if (a[x] + 1 <= n && pos[a[x] + 1] > x && pos[a[x] + 1] <= y)
        updata(a[x] + 1, 1);
      if (a[x] - 1 > 0 && pos[a[x] - 1] > x && pos[a[x] - 1] <= y)
        updata(a[x], -1);
      if (a[y] + 1 <= n && a[y] + 1 != a[x] && pos[a[y] + 1] >= x &&
          pos[a[y] + 1] < y)
        updata(a[y] + 1, -1);
      if (a[y] - 1 > 0 && a[y] - 1 != a[x] && pos[a[y] - 1] >= x &&
          pos[a[y] - 1] < y)
        updata(a[y], 1);
      int t1 = a[x], t2 = a[y];
      a[x] = t2;
      a[y] = t1;
      pos[t1] = y;
      pos[t2] = x;
    }
  }
  return 0;
}
