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;
}
朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。

浙公网安备 33010602011771号