PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000226. Giao của hai hoán vị (Intersection of Permutations)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giao của hai hoán vị (Intersection of Permutations)

Intersection of Permutations

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two permutations aa and bb, each of length nn. Every integer from 11 to nn occurs exactly once in each permutation. Both permutations use 11-based positions.

Process mm operations in the given order:

  • 1 la ra lb rb: Consider the values at positions lala through rara of aa and those at positions lblb through rbrb of bb. Output the number of distinct values belonging to both segments. The two intervals refer to their respective permutations and need not have equal lengths.
  • 2 x y: Swap the values at positions xx and yy of permutation bb. Permutation aa never changes.

Each type-1 query uses the current permutations, including all swaps preceding it.

Input

  • The first line contains integers n,mn,m: the common permutation length and operation count.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in the order of permutation aa.
  • The third line contains exactly nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n in the order of permutation bb.
  • Each of the next mm lines describes an operation, either 1 la ra lb rb or 2 x y, as defined above. All positions are 11-based.

Output

For each type-1 operation, print one integer: the number of values in both queried segments. Preserve query order, one result per line.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le 30; 1≤m≤601\le m\le 60; both aa and bb are permutations of 1,2,…,n1,2,\ldots,n; for type 1, 1≤la≤ra≤n1\le la\le ra\le n and 1≤lb≤rb≤n1\le lb\le rb\le n; for type 2, 1≤x,y≤n1\le x,y\le n and x≠yx\ne y; there is at least one type-1 operation.
  • Subtask 2 (30 points): 2≤n≤10002\le n\le 1000; 1≤m≤30001\le m\le 3000; both aa and bb are permutations of 1,2,…,n1,2,\ldots,n; for type 1, 1≤la≤ra≤n1\le la\le ra\le n and 1≤lb≤rb≤n1\le lb\le rb\le n; for type 2, 1≤x,y≤n1\le x,y\le n and x≠yx\ne y; there is at least one type-1 operation.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤m≤2000001\le m\le 200000; both aa and bb are permutations of 1,2,…,n1,2,\ldots,n; for type 1, 1≤la≤ra≤n1\le la\le ra\le n and 1≤lb≤rb≤n1\le lb\le rb\le n; for type 2, 1≤x,y≤n1\le x,y\le n and x≠yx\ne y; there is at least one type-1 operation.

Examples

Input

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

Output

1
1
1
2
0

Explanation

Initially, a=[5,1,4,2,3,6]a=[5,1,4,2,3,6] and b=[2,5,3,1,4,6]b=[2,5,3,1,4,6].

  • The first query compares a[1..2]=[5,1]a[1..2]=[5,1] with b[4..5]=[1,4]b[4..5]=[1,4]. Only 11 is shared, so the answer is 1.
  • Swapping b2b_2 and b4b_4 produces b=[2,1,3,5,4,6]b=[2,1,3,5,4,6]. The next query compares the sets {5,1}\{5,1\} and {5,4}\{5,4\}, yielding 1.
  • The third query compares a[2..3]=[1,4]a[2..3]=[1,4] and b[3..5]=[3,5,4]b[3..5]=[3,5,4]; they share only 44, giving 1.
  • The fourth query compares all of aa to b[1..2]=[2,1]b[1..2]=[2,1]. Both 22 and 11 occur in aa, giving 2.
  • Swapping b4b_4 and b1b_1 makes b=[5,1,3,2,4,6]b=[5,1,3,2,4,6]. The final query compares a[4..4]=[2]a[4..4]=[2] and b[1..3]=[5,1,3]b[1..3]=[5,1,3]; the intersection is empty, giving 0.

Hence the five printed values are 1, 1, 1, 2, 0.