您的位置:首页 > 其它

pku 3067 Japan 树状数组求逆序数

2013-04-22 23:20 393 查看
/*
之前一直求的是正序数
绕过过来错了好几遍
因为像44312这种数据
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int k,m,n;
int c[1000010];
struct node
{
int x,y;
};
node p[1000010];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int i)
{
while(i<=m)
{
c[i]++;
i+=lowbit(i);
}
}
int query(int i)
{
int sum=0;
while(i>0)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
int t,i;
__int64 ans;
int cnt=1;
scanf("%d",&t);
while(t--)
{
ans=0;
memset(c,0,sizeof(c));
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=k;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+k,cmp);
for(i=1;i<=k;i++)
{
ans+=i-1-query(p[i].y);
update(p[i].y);
}
//ans=m*(m-1)/2-ans;
printf("Test case %d: %I64d\n",cnt++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: