在洛谷上看到的:https://www.luogu.org/problemnew/show/UVA1513
没有UVA账号,并没有提交,感觉代码没问题
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int bit[200009];
int pos[100009];
int n;
int lowbit(int x)
{
return (-x)&x;
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=bit[i];
i-=lowbit(i);
}
return s;
}
void add(int i,int x)
{
while(i<=2*n)///记住不是n
{
bit[i]+=x;
i+=lowbit(i);
}
}
void count1()
{
for(int i=1;i<=n;i++)
printf("%d* ",pos[i]);
printf("\n");
}
int main()
{
int t,m;
scanf("%d",&t);
while(t--)
{
memset(pos,0,sizeof(pos));
memset(bit,0,sizeof(bit));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
pos[i]=n-i+1;///逆向
add(pos[i],1);///位置加一
}
int cnt=n;
int x,ans;
for(int j=0;j<m;j++)
{
scanf("%d",&x);
/// count1();
/// printf("%d***\n",pos[x]);
///printf("%d***\n",sum(pos[x]));///sum()出错了 求位置前数之和
ans=n-sum(pos[x]);
add(pos[x],-1);
pos[x]=++cnt;
add(pos[x],1);
printf("%d\n",ans);
}
}
return 0;
}