#include <bits/stdc++.h>
using namespace std;
int read()
{
int w=1,x=0;
char c = getchar();
while(c<'0'||c>'9'){if(c == '-')w=-1;c=getchar();}
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+c-'0',c=getchar();
return w * x;
}
void write(int x)
{
if(x<0)x=-x,putchar('-');
if(x>9)write(x/10);
putchar(x%10+'0');
}
void writeln(int x)
{
write(x);
putchar(10);
}
const int N = 1e5+5;
int a[N],b[N],n,m,o[N];
int main()
{
int T = read();
while(T--)
{
int last = 0;
n = read(),m = read();
int ans=0;
for(int i{1};i<=n;i++) a[i] = read(),o[a[i]] = i;
for(int i{1};i<=m;i++)
{
int now=0;
now = read();
if(last > o[now]) ans++;
else ans += (o[now]-i) * 2 + 1,last=o[now];
}
writeln(ans);
}
return 0;
}
这段代码对于 这个样例 会出现 TLE 的情况,然将第 37 行的 now = read(); 改为 cin>>now; 就可以正常运行。
据我调试,发现在当对 m 进行循环时,循环到最后一次的输入卡住了,但是为什么。。