SGU - 180 (树状数组+离散化)

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

此题比较坑爹,离散化中,相等的数进行离散化后也需要相等

对于相等数据的离散化,可以采用赋值相同的值来处理

对于相等数据的离散化,可以采用赋值相同的值来处理
#include<iostream>
#include<algorithm>
using namespace std;
__int64 c[70000],n,t[70000];
struct node
{
	__int64 v,f;
}s[70000];
int cmp(const node &a,const node &b)
{
	if(a.v<b.v)
		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,temp,j;
	while(scanf("%I64d",&n)>0)
	{
		memset(c,0,sizeof(c));
		for(i=1;i<=n;i++)
		{
			scanf("%I64d",&s[i].v);
			s[i].f=i;
		}
		sort(s+1,s+n+1,cmp);
		j=0;
		s[0].v=-12;
		for(i=1;i<=n;i++)
		{
			if(s[i].v!=s[i-1].v)
				j++;
			t[s[i].f]=j;
		}
		temp=0;
		for(i=1;i<=n;i++)
		{
				updata(t[i],1);
				temp+=i-sum(t[i]);
		}
		printf("%I64d\n",temp);
	}
	return 0;
}

 

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