#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 9;
long long a[MAX], n, q, p, x, y, f[MAX], pos[MAX], tmp[MAX];
void upd(int x, long long vv) {
  long long v = vv - tmp[x];
  tmp[x] = vv;
  for (; x <= n; x += x & -x) f[x] += v;
}
long long gt(long long x) {
  long long ret = 0;
  for (; x; x -= x & -x) ret += f[x];
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i;
  for (int i = 1; i <= n; i++)
    if (i < pos[a[i] - 1])
      upd(a[i], +1);
    else
      upd(a[i], 0);
  cin >> q;
  while (q--) {
    cin >> p >> x >> y;
    if (p == 1)
      cout << 1 + gt(y) - gt(x) << '\n';
    else {
      swap(a[x], a[y]), pos[a[x]] = x, pos[a[y]] = y;
      if (pos[a[x]] < pos[a[x] - 1])
        upd(a[x], +1);
      else
        upd(a[x], 0);
      if (a[x] != n)
        if (pos[a[x] + 1] < pos[a[x]])
          upd(a[x] + 1, +1);
        else
          upd(a[x] + 1, 0);
      if (pos[a[y]] < pos[a[y] - 1])
        upd(a[y], +1);
      else
        upd(a[y], 0);
      if (a[y] != n)
        if (pos[a[y] + 1] < pos[a[y]])
          upd(a[y] + 1, +1);
        else
          upd(a[y] + 1, 0);
    }
  }
}
