#P1601C. Optimal Insertion
Optimal Insertion
No submission language available for this problem.
Description
You are given two arrays of integers and .
You need to insert all elements of into in an arbitrary way. As a result you will get an array of size .
Note that you are not allowed to change the order of elements in , while you can insert elements of at arbitrary positions. They can be inserted at the beginning, between any elements of , or at the end. Moreover, elements of can appear in the resulting array in any order.
What is the minimum possible number of inversions in the resulting array ? Recall that an inversion is a pair of indices such that and .
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains two integers and ().
The second line of each test case contains integers ().
The third line of each test case contains integers ().
It is guaranteed that the sum of for all tests cases in one input doesn't exceed . The sum of for all tests cases doesn't exceed as well.
For each test case, print one integer — the minimum possible number of inversions in the resulting array .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains two integers and ().
The second line of each test case contains integers ().
The third line of each test case contains integers ().
It is guaranteed that the sum of for all tests cases in one input doesn't exceed . The sum of for all tests cases doesn't exceed as well.
Output
For each test case, print one integer — the minimum possible number of inversions in the resulting array .
Samples
Note
Below is given the solution to get the optimal answer for each of the example test cases (elements of are underscored).
- In the first test case, .
- In the second test case, .
- In the third test case, .