#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int D[N], A[N], IND[N], n, q, tp;
void add(int ind) {
  for (ind++; ind < N; ind += (ind & (-ind))) D[ind]++;
}
void del(int ind) {
  for (ind++; ind < N; ind += (ind & (-ind))) D[ind]--;
}
int get(int ind) {
  int sm = 0;
  for (ind++; ind; ind -= (ind & (-ind))) sm += D[ind];
  return (sm);
}
int get(int l, int r) { return (get(r) - get(l - 1)); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  for (int i = 1; i <= n; i++) IND[A[i]] = i;
  for (int i = 2; i <= n; i++)
    if (IND[i] > IND[i - 1]) add(i);
  scanf("%d", &q);
  for (int Q = 0; Q < q; Q++) {
    int x, y;
    scanf("%d %d %d", &tp, &x, &y);
    if (tp == 1) {
      printf("%d\n", y - x + 1 - get(x + 1, y));
      continue;
    }
    int a = A[x];
    vector<int> st1;
    st1.push_back(-1);
    if (a > 1 && IND[a] > IND[a - 1]) st1.push_back(a);
    if (a < n && IND[a] < IND[a + 1]) st1.push_back(a + 1);
    a = A[y];
    if (a > 1 && IND[a] > IND[a - 1]) st1.push_back(a);
    if (a < n && IND[a] < IND[a + 1]) st1.push_back(a + 1);
    sort(st1.begin(), st1.end());
    for (int i = 1; i < (int)st1.size(); i++)
      if (st1[i] ^ st1[i - 1]) del(st1[i]);
    swap(A[x], A[y]);
    IND[A[x]] = x;
    IND[A[y]] = y;
    vector<int> st2;
    st2.push_back(-1);
    a = A[x];
    if (a > 1 && IND[a] > IND[a - 1]) st2.push_back(a);
    if (a < n && IND[a] < IND[a + 1]) st2.push_back(a + 1);
    a = A[y];
    if (a > 1 && IND[a] > IND[a - 1]) st2.push_back(a);
    if (a < n && IND[a] < IND[a + 1]) st2.push_back(a + 1);
    sort(st2.begin(), st2.end());
    for (int i = 1; i < (int)st2.size(); i++)
      if (st2[i] ^ st2[i - 1]) add(st2[i]);
  }
}
