Permutations

4.8

4 votes
Advanced Data Structures, Data Structures, Hard, Segment Trees, Tries
Problem

You are given a permutation A={a1,a2,a3,...aN} of N integers from 0 to N1.

You are also given Q queries of the following two forms:

  • 1  X Y: Swap (aX,aY)
  • 2  L R K: Print MexK (aL,,aR)

Here, MexK(aL,,aR) = Kth smallest non-negative integer that is not available in the subarray (aL,,aR).

You can answer the next query only when you answer the previous one. You must answer these queries online.

Input format

  • First line: A single integer N denoting the number of elements in the permutation
  • Second line: N space-separated integers a1,a2,a3,...aN
  • Next Q lines: Each describing a query

As you are required to respond to the queries online, the queries are encoded.

  • A query of the first type is provided in the following format:
    • 1  X Y.
  • A query of the second type is provided in the following format:
    • 2  L R K.

You can decode the queries as follows:

X=XLastAns,Y=YLastAns,L=LLastAns,R=RLastAns,K=KLastAns

Here, LastAns is the last answer to the query of the second type. Initially, LastAns=0.

Output format

For each query, print the answer in a single line

Constraints

1N105

1Q2105

1X,YN

1LRN

1K105

0ai<N

Sample Input
4
0 3 1 2
4
2 2 3 1
1 1 2
2 2 3 1
2 3 6 0
Sample Output
0
2
5
Time Limit: 5
Memory Limit: 512
Source Limit:
Explanation

Initially, A = {0,3,1,2} and LastAns = 0

  • Query of 2nd type L=2,R=3,K=1 : LastAns = 0
  • Query of 1st type X=1,Y=2.  Permutation A changed to : {3,0,1,2}
  • Query of 2nd type L=2,R=3,K=1 : LastAns = 2
  • Query of 2nd type L=1,R=4,K=2 : LastAns = 5
Editor Image

?