poj2299(离散化+树状数组)

http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=15025

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	__int64 f,v;
}str[500000];
__int64 c[500000],n,t[500000];
int cmp(const node &a,const node &b)
{
	if(a.f<b.f)
		return 1;
	else
		return 0;
}
__int64 lowbit(__int64 x)
{
	return x&(-x);
}
__int64 sum(__int64 x)
{
	__int64 s=0;
	while(x>0)
	{
		s+=c[x];
		x-=lowbit(x);
	}
	return s;
}
void updata(__int64 i,__int64 j)
{
	while(i<=n)
	{
		c[i]+=j;
		i+=lowbit(i);
	}
}
int main()
{
	__int64 i,ans;
	while(scanf("%I64d",&n)>0&&n)
	{
		c[0]=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&str[i].f);
			str[i].f++;
			str[i].v=i;
			c[i]=0;
		}
		sort(str+1,str+1+n,cmp);
		for(i=1;i<=n;i++)
			t[str[i].v]=i;
		ans=0;
		for(i=1;i<=n;i++)
		{
			updata(t[i],1);
			ans+=i-sum(t[i]);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

 

posted @ 2012-11-22 16:31  紫忆  阅读(304)  评论(0编辑  收藏  举报