Stack of Presents
维护最深深度即可
比如先拿一个第5层的,这时最深深度就是5,接下来如果拿第4层的就可以认为它刚刚已经被移到了最上面,如果要拿第7层的,就需要把上面的移出,然后更新深度
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x7f7f7f7f;
const double eps = 1e-5;
const double Pi = acos(-1);
const int maxn = 1e5 + 5;
map<int, int> mp;
int an[maxn];
int main(void) {
int T;
read(T);
while (T--) {
mp.clear();
int n, m;
read(n), read(m);
for (int i = 1; i <= n; i++) {
int x;
read(x);
mp[x] = i;
}
for (int i = 1; i <= m; i++)
read(an[i]);
ll ans = 0;
int maxx = 1;
for (int i = 1; i <= m; i++) {
if (mp[an[i]] > maxx) {
ans += (ll)(mp[an[i]] - i) * 2 + 1;
maxx = mp[an[i]];
} else
ans++;
}
printf("%lld\n", ans);
}
return 0;
}