#include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
const int INF = 1000000000;
const int MAX = 350000;
const int MAX2 = 3007;
const int MAX3 = 1100000;
const int BASE = 1000000000;
const double EPS = 1E-7;
int n, m;
int A[MAX];
int P[MAX];
int T[MAX * 4];
int Sum(int l, int r, int i, int tl, int tr) {
  if (l > r) return 0;
  if (l == tl && r == tr) return T[i];
  int tm = (tl + tr) / 2;
  if (r <= tm) return Sum(l, r, i * 2, tl, tm);
  if (l > tm) return Sum(l, r, i * 2 + 1, tm + 1, tr);
  return Sum(l, tm, i * 2, tl, tm) + Sum(tm + 1, r, i * 2 + 1, tm + 1, tr);
}
void Set(int j, int d, int i, int l, int r) {
  if (l == r)
    T[i] = d;
  else {
    int tm = (l + r) / 2;
    if (j <= tm)
      Set(j, d, i * 2, l, tm);
    else
      Set(j, d, i * 2 + 1, tm + 1, r);
    T[i] = T[i * 2] + T[i * 2 + 1];
  }
}
void Update(int x) {
  if (x == 0) Set(x, 1, 1, 0, n - 1);
  if (x + 1 < n && P[x + 1] < P[x])
    Set(x + 1, 1, 1, 0, n - 1);
  else if (x + 1 < n)
    Set(x + 1, 0, 1, 0, n - 1);
}
void Swap(int a, int b) {
  vector<int> I;
  for (int d = (-2); d < (3); ++d)
    if (A[a] + d >= 0 && A[a] + d < n) I.push_back(A[a] + d);
  for (int d = (-2); d < (3); ++d)
    if (A[b] + d >= 0 && A[b] + d < n) I.push_back(A[b] + d);
  swap(P[A[a]], P[A[b]]);
  swap(A[a], A[b]);
  for (int i = (0); i < ((int)I.size()); ++i) Update(I[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) {
    scanf("%d", &A[i]);
    --A[i];
    P[A[i]] = i;
  }
  for (int i = (0); i < (n); ++i) {
    if (i == 0) Set(0, 1, 1, 0, n - 1);
    if (i + 1 < n && P[i + 1] < P[i]) Set(i + 1, 1, 1, 0, n - 1);
  }
  scanf("%d", &m);
  for (int i = (0); i < (m); ++i) {
    int q, a, b;
    scanf("%d %d %d", &q, &a, &b);
    --a;
    --b;
    if (q == 1) {
      printf("%d\n", Sum(a + 1, b, 1, 0, n - 1) + 1);
    } else {
      Swap(a, b);
    }
  }
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily!

There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, that ai1 = x, ai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.

If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y] (x ≤ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.

All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: 

  * what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive? 
  * two beavers on positions x and y (the beavers ax and ay) swapped. 



You can assume that any beaver can be shaved any number of times.

Input

The first line contains integer n — the total number of beavers, 2 ≤ n. The second line contains n space-separated integers — the initial beaver permutation.

The third line contains integer q — the number of queries, 1 ≤ q ≤ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≤ xi < yi ≤ n.

  * to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem B1); 
  * to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems B1+B2). 



Note that the number of queries q is limited 1 ≤ q ≤ 105 in both subproblem B1 and subproblem B2.

Output

For each query with pi = 1, print the minimum number of Beavershave 5000 sessions.

Examples

Input

5
1 3 4 2 5
6
1 1 5
1 3 4
2 2 3
1 1 5
2 1 5
1 1 5


Output

2
1
3
5

### Response

```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
const int INF = 1000000000;
const int MAX = 350000;
const int MAX2 = 3007;
const int MAX3 = 1100000;
const int BASE = 1000000000;
const double EPS = 1E-7;
int n, m;
int A[MAX];
int P[MAX];
int T[MAX * 4];
int Sum(int l, int r, int i, int tl, int tr) {
  if (l > r) return 0;
  if (l == tl && r == tr) return T[i];
  int tm = (tl + tr) / 2;
  if (r <= tm) return Sum(l, r, i * 2, tl, tm);
  if (l > tm) return Sum(l, r, i * 2 + 1, tm + 1, tr);
  return Sum(l, tm, i * 2, tl, tm) + Sum(tm + 1, r, i * 2 + 1, tm + 1, tr);
}
void Set(int j, int d, int i, int l, int r) {
  if (l == r)
    T[i] = d;
  else {
    int tm = (l + r) / 2;
    if (j <= tm)
      Set(j, d, i * 2, l, tm);
    else
      Set(j, d, i * 2 + 1, tm + 1, r);
    T[i] = T[i * 2] + T[i * 2 + 1];
  }
}
void Update(int x) {
  if (x == 0) Set(x, 1, 1, 0, n - 1);
  if (x + 1 < n && P[x + 1] < P[x])
    Set(x + 1, 1, 1, 0, n - 1);
  else if (x + 1 < n)
    Set(x + 1, 0, 1, 0, n - 1);
}
void Swap(int a, int b) {
  vector<int> I;
  for (int d = (-2); d < (3); ++d)
    if (A[a] + d >= 0 && A[a] + d < n) I.push_back(A[a] + d);
  for (int d = (-2); d < (3); ++d)
    if (A[b] + d >= 0 && A[b] + d < n) I.push_back(A[b] + d);
  swap(P[A[a]], P[A[b]]);
  swap(A[a], A[b]);
  for (int i = (0); i < ((int)I.size()); ++i) Update(I[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) {
    scanf("%d", &A[i]);
    --A[i];
    P[A[i]] = i;
  }
  for (int i = (0); i < (n); ++i) {
    if (i == 0) Set(0, 1, 1, 0, n - 1);
    if (i + 1 < n && P[i + 1] < P[i]) Set(i + 1, 1, 1, 0, n - 1);
  }
  scanf("%d", &m);
  for (int i = (0); i < (m); ++i) {
    int q, a, b;
    scanf("%d %d %d", &q, &a, &b);
    --a;
    --b;
    if (q == 1) {
      printf("%d\n", Sum(a + 1, b, 1, 0, n - 1) + 1);
    } else {
      Swap(a, b);
    }
  }
  return 0;
}

```