Stack of Presents CodeForces - 1279C

Santa has to send presents to the kids. He has a large stack of nn presents, numbered from 11 to nn; the topmost present has number a1a1, the next present is a2a2, and so on; the bottom present has number anan. All numbers are distinct.

Santa has a list of mdistinct presents he has to send: b1b1, b2b2, ..., bmbm. He will send them in the order they appear in the list.

To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are kk presents above the present Santa wants to send, it takes him 2k+12k+1 seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).

What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.

Your program has to answer tt different test cases.

Input

The first line contains one integer tt (1t1001≤t≤100) — the number of test cases.

Then the test cases follow, each represented by three lines.

The first line contains two integers nn and mm (1mn1051≤m≤n≤105) — the number of presents in the stack and the number of presents Santa wants to send, respectively.

The second line contains nn integers a1a1, a2a2, ..., anan (1ain1≤ai≤n, all aiai are unique) — the order of presents in the stack.

The third line contains mm integers b1b1, b2b2, ..., bmbm (1bin1≤bi≤n, all bibi are unique) — the ordered list of presents Santa has to send.

The sum of nn over all test cases does not exceed 105105.

Output

For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.

Example


Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
这道题其实也很好做,就是每次找到要拿出来的礼物的位置,如果比之前的都大,那么时间是1 + 2*pos(因为pos我们从0开始,所以直接用pos的值没有减一),在放回去的时候,按照从拿出来的礼物中找出后面的序列要的礼物,并且按照要的顺序排放在栈顶,这样在遇到这些礼物请求时都只要1s,所以我们只要记录之前的最大值就行了,在遇到的编号小于最大值时,在拿出最大值那一次就已经把后面的序列按顺序排好了,所以都是1s,唯独遇到比最大值大的要用1+2*(pos - i)(i为之前拿走的个数,因为现在的位置比之前最大还大,所以之前的i个都在前面拿走了,现在的位置,前面有的礼物数应当是现在位置数pos减去之前拿走了i个,即pos - i(再次强调如果pos从0开始,才算pos - i, 如果从一开始计数就是pos - i - 1, 因为是算该位置之前的*2再加上该位置的1)。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int a[100005], pos[100005];
int main(void)
{
	int t, temp;
	int m, n;
	scanf("%d", &t);
	for(int cnt = 0; cnt < t; cnt++)
	{
		long long ans;//假设每次都2*pos,2*100000*100000(虽然这种情况不可能达到但是大致可以估计会超出int
		int last_max_order = -1;//之前最大的位置
		scanf("%d %d", &n, &m);
		ans = m;//预先把每一个的1s都算好,到时候不是最大直接跳过,最大的话+2*(pos  -  i)
		for(int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			pos[a[i]] = i;//值与位置一一映射,O(1)方法查找否则超时
		}
		for(int i = 0; i < m; i++)
		{
			scanf("%d", &temp);
			if(pos[temp] > last_max_order)
			{
				ans += (pos[temp] - i) * 2;
				last_max_order = pos[temp];//记得更新最大值
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}
 
  

  

 

猜你喜欢

转载自www.cnblogs.com/jacobfun/p/12347394.html