#include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 10;
struct que {
  int a, l, r;
} q[N];
int a[N], whe[N];
int ans[N];
int dp[N], n;
int c[N];
void add(int u, int val) {
  while (u < N) {
    c[u] += val;
    u += ((u) & (-u));
  }
}
int ask(int u) {
  int ret = 0;
  while (u) {
    ret += c[u];
    u -= ((u) & (-u));
  }
  return ret;
}
void fix(int u) {
  if (u == 1) return;
  if (u > n) return;
  int now;
  int last = ask(u) - ask(u - 1);
  if (whe[u] < whe[u - 1])
    now = 1;
  else if (whe[u] > whe[u - 1])
    now = 0;
  add(u, now - last);
}
void solve(int l, int r) {
  if (l == r) {
    if (q[l].a == 1)
      ans[l] = ask(q[l].r) - ask(q[l].l) + 1;
    else {
      swap(whe[a[q[l].l]], whe[a[q[l].r]]);
      swap(a[q[l].l], a[q[l].r]);
      fix(a[q[l].l]);
      fix(a[q[l].l] + 1);
      fix(a[q[r].r]);
      fix(a[q[r].r] + 1);
    }
    return;
  }
  int mid = (l + r) >> 1;
  solve(l, mid);
  if (mid + 1 <= r) solve(mid + 1, r);
}
int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    whe[a[i]] = i;
  }
  memset(c, 0, sizeof(c));
  for (i = 2; i <= n; ++i)
    if (whe[i - 1] > whe[i]) add(i, 1);
  int Q;
  scanf("%d", &Q);
  for (i = 1; i <= Q; ++i) scanf("%d%d%d", &q[i].a, &q[i].l, &q[i].r);
  solve(1, Q);
  for (i = 1; i <= Q; ++i)
    if (q[i].a == 1) printf("%d\n", ans[i]);
  return 0;
}
