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
import java.io.*;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: DOAN Minh Quy
 * Date: 7/17/13
 * Time: 10:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class TaskB {
    public static void main(String[] args){
        new TaskB().run();
    }
    int bit[], n;
    int get(int x){
        int sum = 0;
        while(x>0){
            sum += bit[x];
            x -= (x&-x);
           // System.out.println("x = "+x);
        }
        return sum;
    }
    void update(int x,int val){
        while(x<=n){
            bit[x] += val;
            x += (x&-x);
        }
    }
    void run(){
        TaskBReader reader = new TaskBReader(System.in);
        TaskBWriter writer = new TaskBWriter(System.out);
        n = reader.nextInt();
        bit = new int[n+1];
        int[] est = new int[n+1];
        int[] rev = new int[n+1];
        for(int i = 1 ; i <= n ; ++i){
            est[i] = reader.nextInt();
            rev[est[i]] = i;
        }
        rev[0] = -1;
        for(int i = 1 ; i <= n ; ++i){
            if ( rev[i] < rev[i-1] ){
                update(i,1);
            }
        }
//        for(int i = 1 ; i <= n ; ++i){
//            writer.print(rev[i]+" ");
//        }
//        writer.println();
        int q = reader.nextInt();
        for(int k = 1 ; k <= q ; ++k){
            int p = reader.nextInt();
            int x = reader.nextInt();
            int y = reader.nextInt();
            if ( p == 1 ){
                writer.println(get(y)-get(x)+1);
            }else{
                Set<Integer> set = new TreeSet<Integer>();
                if ( rev[est[x]] < rev[est[x]-1] ){
                    set.add(est[x]);
                }
                if ( est[x] < n && rev[est[x]+1] < rev[est[x]] ){
                    set.add(est[x]+1);
                }
                if ( rev[est[y]] < rev[est[y]-1] ){
                    set.add(est[y]);
                }
                if ( est[y] < n && rev[est[y]+1] < rev[est[y]] ){
                    set.add(est[y]+1);
                }
                for(Integer sub :set){
                    update(sub,-1);
                }
                int sw = est[x]; est[x] = est[y] ; est[y] = sw;
                rev[est[x]] = x;
                rev[est[y]] = y;
                set.clear();
                if ( rev[est[x]] < rev[est[x]-1] ){
                    set.add(est[x]);
                }
                if ( est[x] < n && rev[est[x]+1] < rev[est[x]] ){
                    set.add(est[x]+1);
                }
                if ( rev[est[y]] < rev[est[y]-1] ){
                    set.add(est[y]);
                }
                if ( est[y] < n && rev[est[y]+1] < rev[est[y]] ){
                    set.add(est[y]+1);
                }
                for(Integer sub :set){
                    update(sub,1);
                }
            }
        }
        writer.close();
    }
}
class TaskBReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public TaskBReader (InputStream stream) {
        this.stream = stream;
    }

    public int read () {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read (buf);
            } catch (IOException e) {
                throw new InputMismatchException ();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt () {
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read ();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException ();
            res *= 10;
            res += c - '0';
            c = read ();
        } while (!isSpaceChar (c));
        return res * sgn;
    }
    public boolean isSpaceChar (int c) {
        if (filter != null)
            return filter.isSpaceChar (c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar (int ch);
    }

}

class TaskBWriter {
    private final PrintWriter writer;

    public TaskBWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }
    public void println(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

}
