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 a 1, a 2, …, a n 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 i 1 < i 2 < … < i k, that a i 1 = x, a i 2 = x + 1, …, a i k - 1 = y - 1, a i k = 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, p 1], [p 1 + 1, p 2], …, [p m + 1, y] (x ≤ p 1 < p 2 < … < p m < 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 a x and a y) 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 p i x i y i, where p i is the query type (1 is to shave beavers from x i to y i, inclusive, 2 is to swap beavers on positions x i and y i). All queries meet the condition: 1 ≤ x i < y i ≤ 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 p i = 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
Solution:
#include <iostream> #include <iomanip> #include <stdio.h> #include <set> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <memory.h> #include <string> #include <sstream> #include <cstdlib> #include <ctime> #include <cassert> using namespace std; int n, s[333333], pz[333333], have[333333], p[333333]; void modify(int x, int v) { while (x <= n) { s[x] += v; x = (x | (x-1))+1; } } int findsum(int x) { int v = 0; while (x > 0) { v += s[x]; x &= x-1; } return v; } void put(int x, int v) { if (have[x] != v) { modify(x, v-have[x]); have[x] = v; } } int main() { scanf("%d", &n); for (int i=1;i<=n;i++) { int foo; scanf("%d", &foo); p[i] = foo; pz[foo] = i; } for (int i=1;i<=n;i++) s[i] = have[i] = 0; for (int i=1;i<n;i++) if (pz[i] > pz[i+1]) put(i, 1); int tt; scanf("%d", &tt); while (tt--) { int com, x, y; scanf("%d %d %d", &com, &x, &y); if (com == 1) { printf("%d\n", findsum(y-1) - findsum(x-1) + 1); } else { int tmp = p[x]; p[x] = p[y]; p[y] = tmp; pz[p[x]] = x; pz[p[y]] = y; { int z = x; if (p[z] > 1) if (pz[p[z]-1] > pz[p[z]]) put(p[z]-1, 1); else put(p[z]-1, 0); if (p[z] < n) if (pz[p[z]] > pz[p[z]+1]) put(p[z], 1); else put(p[z], 0); } { int z = y; if (p[z] > 1) if (pz[p[z]-1] > pz[p[z]]) put(p[z]-1, 1); else put(p[z]-1, 0); if (p[z] < n) if (pz[p[z]] > pz[p[z]+1]) put(p[z], 1); else put(p[z], 0); } } } return 0; }