#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000 + 100;
int n, m, k, a, b, sq[MAXN], ps[MAXN], fh[MAXN];
int A[MAXN] = {};
void add(int x, int y) {
  for (; x > 0 && x <= n; x += x & -x) A[x] += y;
}
int sum(int x) {
  int res = 0;
  for (; x > 0; x -= x & -x) res += A[x];
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &sq[i]), ps[sq[i]] = i;
  fh[1] = 0;
  for (int i = 2; i <= n; i++) fh[i] = (ps[i - 1] > ps[i]), add(i, fh[i]);
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d%d", &k, &a, &b);
    if (k == 1) {
      printf("%d\n", sum(b) - sum(a) + 1);
    } else {
      int va = sq[a], vb = sq[b];
      ps[va] = b, ps[vb] = a;
      sq[a] = vb, sq[b] = va;
      if (va > 1)
        add(va, (ps[va - 1] > ps[va]) - fh[va]), fh[va] = ps[va - 1] > ps[va];
      if (va < n)
        add(va + 1, (ps[va] > ps[va + 1]) - fh[va + 1]),
            fh[va + 1] = ps[va] > ps[va + 1];
      va = vb;
      if (va > 1)
        add(va, (ps[va - 1] > ps[va]) - fh[va]), fh[va] = ps[va - 1] > ps[va];
      if (va < n)
        add(va + 1, (ps[va] > ps[va + 1]) - fh[va + 1]),
            fh[va + 1] = ps[va] > ps[va + 1];
    }
  }
  return 0;
}
