#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
using namespace std;
template <typename T1, typename T2, typename T3>
struct triple {
  T1 a;
  T2 b;
  T3 c;
  triple(){};
  triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {
  return t1.a < t2.a;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(std::ostream &os, const triple<T1, T2, T3> &tr) {
  os << "{" << tr.a << "," << tr.b << "," << tr.c << "}";
  return os;
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < v.size(); i++) {
    if (!first) os << ", ";
    os << v[i];
    first = false;
  }
  return os << "]";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
  bool first = true;
  os << "[";
  for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ", ";
    os << *ii;
    first = false;
  }
  return os << "]";
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
  bool first = true;
  os << "[";
  for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ", ";
    os << *ii;
    first = false;
  }
  return os << "]";
}
template <typename T, typename T2>
void printarray(T a[], T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
}
inline long long binpow(long long x, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long x, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * x) % _mod;
    x = (x * x) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
inline long long lcm(long long a, long long b, long long c) {
  return lcm(lcm(a, b), c);
}
inline long long lcm(int a, int b, int c) {
  return lcm(lcm(a, b), (long long)c);
}
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline double max(double a, double b) { return (a > b) ? a : b; }
inline long long max(long long a, long long b, long long c) {
  return max(a, max(b, c));
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline long long min(long long a, long long b) { return (a < b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline long long min(long long a, long long b, long long c) {
  return min(a, min(b, c));
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
inline void getar(int *a, int n) {
  for (int ii = 0; ii < n; ii++) {
    scanf("%d", a + ii);
  }
}
inline void getar(long long *a, int n) {
  for (int ii = 0; ii < n; ii++) {
    scanf("%I64", a + ii);
  }
}
int b[300010];
int a[300010];
int tr[600010];
int pos[600010];
void init(int cur, int l, int r) {
  if (l == r) {
    tr[cur] = 1;
    return;
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  init(dcur, l, m);
  init(dcur + 1, m + 1, r);
  tr[cur] = tr[dcur] + tr[dcur + 1];
  if (b[m + 1] > b[m]) tr[cur]--;
}
void update(int cur, int l, int r, int x, int y) {
  if (l == r) {
    b[x] = y;
    return;
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  if (x <= m) {
    update(dcur, l, m, x, y);
  } else {
    update(dcur + 1, m + 1, r, x, y);
  }
  tr[cur] = tr[dcur] + tr[dcur + 1];
  if (b[m + 1] > b[m]) tr[cur]--;
}
int get(int cur, int l, int r, int x, int y) {
  if (x > r) return 0;
  if (y < l) return 0;
  if (x < l) x = l;
  if (y > r) y = r;
  if (x == l && y == r) {
    return tr[cur];
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  if (y <= m)
    return get(dcur, l, m, x, y);
  else if (x > m)
    return get(dcur + 1, m + 1, r, x, y);
  int res = get(dcur, l, m, x, y) + get(dcur + 1, m + 1, r, x, y);
  if (b[m + 1] > b[m]) res--;
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    b[x] = i;
  }
  init(1, 1, n);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    int t;
    scanf("%d%d%d", &t, &x, &y);
    if (t == 1) {
      int r = get(1, 1, n, x, y);
      printf("%d\n", r);
    } else {
      update(1, 1, n, a[x], y);
      update(1, 1, n, a[y], x);
      swap(a[x], a[y]);
    }
  }
  return 0;
}

### Prompt

Generate a Cpp solution to the following problem:

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:20000000")
using namespace std;
template <typename T1, typename T2, typename T3>
struct triple {
  T1 a;
  T2 b;
  T3 c;
  triple(){};
  triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {
  return t1.a < t2.a;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(std::ostream &os, const triple<T1, T2, T3> &tr) {
  os << "{" << tr.a << "," << tr.b << "," << tr.c << "}";
  return os;
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < v.size(); i++) {
    if (!first) os << ", ";
    os << v[i];
    first = false;
  }
  return os << "]";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
  bool first = true;
  os << "[";
  for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ", ";
    os << *ii;
    first = false;
  }
  return os << "]";
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
  bool first = true;
  os << "[";
  for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ", ";
    os << *ii;
    first = false;
  }
  return os << "]";
}
template <typename T, typename T2>
void printarray(T a[], T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
}
inline long long binpow(long long x, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long x, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * x) % _mod;
    x = (x * x) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
inline long long lcm(long long a, long long b, long long c) {
  return lcm(lcm(a, b), c);
}
inline long long lcm(int a, int b, int c) {
  return lcm(lcm(a, b), (long long)c);
}
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline double max(double a, double b) { return (a > b) ? a : b; }
inline long long max(long long a, long long b, long long c) {
  return max(a, max(b, c));
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline long long min(long long a, long long b) { return (a < b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline long long min(long long a, long long b, long long c) {
  return min(a, min(b, c));
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
inline void getar(int *a, int n) {
  for (int ii = 0; ii < n; ii++) {
    scanf("%d", a + ii);
  }
}
inline void getar(long long *a, int n) {
  for (int ii = 0; ii < n; ii++) {
    scanf("%I64", a + ii);
  }
}
int b[300010];
int a[300010];
int tr[600010];
int pos[600010];
void init(int cur, int l, int r) {
  if (l == r) {
    tr[cur] = 1;
    return;
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  init(dcur, l, m);
  init(dcur + 1, m + 1, r);
  tr[cur] = tr[dcur] + tr[dcur + 1];
  if (b[m + 1] > b[m]) tr[cur]--;
}
void update(int cur, int l, int r, int x, int y) {
  if (l == r) {
    b[x] = y;
    return;
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  if (x <= m) {
    update(dcur, l, m, x, y);
  } else {
    update(dcur + 1, m + 1, r, x, y);
  }
  tr[cur] = tr[dcur] + tr[dcur + 1];
  if (b[m + 1] > b[m]) tr[cur]--;
}
int get(int cur, int l, int r, int x, int y) {
  if (x > r) return 0;
  if (y < l) return 0;
  if (x < l) x = l;
  if (y > r) y = r;
  if (x == l && y == r) {
    return tr[cur];
  }
  int m = (l + r) >> 1;
  int dcur = cur << 1;
  if (y <= m)
    return get(dcur, l, m, x, y);
  else if (x > m)
    return get(dcur + 1, m + 1, r, x, y);
  int res = get(dcur, l, m, x, y) + get(dcur + 1, m + 1, r, x, y);
  if (b[m + 1] > b[m]) res--;
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    b[x] = i;
  }
  init(1, 1, n);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    int t;
    scanf("%d%d%d", &t, &x, &y);
    if (t == 1) {
      int r = get(1, 1, n, x, y);
      printf("%d\n", r);
    } else {
      update(1, 1, n, a[x], y);
      update(1, 1, n, a[y], x);
      swap(a[x], a[y]);
    }
  }
  return 0;
}

```