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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 and , each of length . Every integer from to occurs exactly once in each permutation. Both permutations use -based positions.
Process operations in the given order:
1 la ra lb rb: Consider the values at positions through of and those at positions through of . 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 and of permutation . Permutation never changes.
Each type-1 query uses the current permutations, including all swaps preceding it.
Input
- The first line contains integers : the common permutation length and operation count.
- The second line contains exactly integers in the order of permutation .
- The third line contains exactly integers in the order of permutation .
- Each of the next lines describes an operation, either
1 la ra lb rbor2 x y, as defined above. All positions are -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): ; ; both and are permutations of ; for type
1, and ; for type2, and ; there is at least one type-1operation. - Subtask 2 (30 points): ; ; both and are permutations of ; for type
1, and ; for type2, and ; there is at least one type-1operation. - Subtask 3 (50 points): ; ; both and are permutations of ; for type
1, and ; for type2, and ; there is at least one type-1operation.
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, and .
- The first query compares with . Only is shared, so the answer is
1. - Swapping and produces . The next query compares the sets and , yielding
1. - The third query compares and ; they share only , giving
1. - The fourth query compares all of to . Both and occur in , giving
2. - Swapping and makes . The final query compares and ; the intersection is empty, giving
0.
Hence the five printed values are 1, 1, 1, 2, 0.