#include <bits/stdc++.h>
using namespace std;
int *a = new int[1048576];
int l(int q) {
  int w = 1, r = 0, t;
  for (t = 262144; t; t >>= 1) {
    w <<= 1;
    if (q >= t) {
      q -= t;
      r += a[w];
      w++;
    }
  }
  return r;
}
void k(int w, int e) {
  for (w += 524288; w; w >>= 1) a[w] += e;
}
int main() {
  int q, w, e, r, t, c, v, s[300000], d[4], *f = new int[300000];
  for (w = 0; w < 1048576; w++) a[w] = 0;
  cin >> q;
  for (w = 0; w < q; w++) {
    scanf("%d", &f[w]);
    f[w]--;
    s[f[w]] = w;
  }
  for (w = 0; w < q - 1; w++)
    if (s[w] > s[w + 1]) k(w, 1);
  for (cin >> w; w; w--) {
    scanf("%d%d%d", &t, &e, &r);
    e--;
    r--;
    if (t == 1)
      printf("%d\n", l(r) - l(e) + 1);
    else {
      if (f[e] > f[r]) {
        t = e;
        e = r;
        r = t;
      }
      t = 0;
      if (f[e] > 0) {
        d[t] = f[e] - 1;
        t++;
      }
      d[t] = f[e];
      t++;
      if (f[e] < f[r] - 1) {
        d[t] = f[r] - 1;
        t++;
      }
      if (f[r] < q - 1) {
        d[t] = f[r];
        t++;
      }
      for (c = 0; c < t; c++)
        if (s[d[c]] > s[d[c] + 1]) k(d[c], -1);
      c = f[e];
      f[e] = f[r];
      f[r] = c;
      s[f[e]] = e;
      s[f[r]] = r;
      for (c = 0; c < t; c++)
        if (s[d[c]] > s[d[c] + 1]) k(d[c], 1);
    }
  }
  return 0;
}
