#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int s[300009 << 2], pos[300009], arr[300009];
pair<int, int> range[300009 << 2];
vector<int> adj[300009];
void build(int nd, int x, int y) {
  if (x == y) {
    s[nd] = 1;
    return;
  }
  int mid = (x + y) >> 1;
  build(nd << 1, x, mid);
  build(nd << 1 | 1, mid + 1, y);
  s[nd] = s[nd << 1] + s[nd << 1 | 1];
  if (pos[mid] < pos[mid + 1]) s[nd]--;
  adj[mid].push_back(nd);
  adj[mid + 1].push_back(nd);
  range[nd] = make_pair(x, y);
}
void upd(int l, int r, int id, int nd, int x, int y) {
  if (l > y or x > r or nd > id) return;
  int mid = (x + y) >> 1;
  upd(l, r, id, nd << 1, x, mid);
  upd(l, r, id, nd << 1 | 1, mid + 1, y);
  s[nd] = s[nd << 1] + s[nd << 1 | 1];
  if (pos[mid] < pos[mid + 1]) s[nd]--;
}
int tap(int l, int r, int nd, int x, int y) {
  if (l > y or x > r) return -1;
  if (l <= x and y <= r) return s[nd];
  int mid = (x + y) >> 1;
  int i1 = tap(l, r, nd << 1, x, mid);
  int i2 = tap(l, r, nd << 1 | 1, mid + 1, y);
  if (i1 == -1) return i2;
  if (i2 == -1) return i1;
  if (pos[mid] < pos[mid + 1]) return i1 + i2 - 1;
  return i1 + i2;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", arr + i), pos[arr[i]] = i;
  build(1, 1, n);
  int q;
  scanf("%d", &q);
  while (q--) {
    int type, l, r;
    scanf("%d%d%d", &type, &l, &r);
    if (type == 2) {
      pos[arr[l]] = r;
      pos[arr[r]] = l;
      swap(arr[l], arr[r]);
      for (__typeof((adj[arr[l]]).begin()) it = (adj[arr[l]]).begin();
           it != (adj[arr[l]]).end(); it++)
        upd(range[*it].first, range[*it].second, *it, 1, 1, n);
      for (__typeof((adj[arr[r]]).begin()) it = (adj[arr[r]]).begin();
           it != (adj[arr[r]]).end(); it++)
        upd(range[*it].first, range[*it].second, *it, 1, 1, n);
    } else
      printf("%d\n", tap(l, r, 1, 1, n));
  }
  return 0;
}
