把stack倒着看,把一个元素放到最顶上即把这个元素放到最低部
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
int c[MAXN], a[MAXN];
int lowbit(int x) { return x&-x; }
void add(int x, int add) {
while(x < MAXN) {
c[x] += add;
x += lowbit(x);
}
}
int sum(int x) {
int ret = 0;
while(x > 0) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
int main() {
int T;
cin >> T;
while(T--) {
int n, m; scanf("%d%d", &n, &m);
memset(c, 0, sizeof(c));
for(int i = 1; i <= n; i++) add(i, 1), a[i] = n-i+1;
int cur = n;
while(m--) {
int t;
scanf("%d", &t);
printf("%d", sum(MAXN-1)-sum(a[t]));
printf("%c", m?' ':'\n');
add(a[t], -1);
a[t] = ++cur;
add(a[t], 1);
}
}
return 0;
}