由于num的值过大,不能直接开a【num】,所以对num进行离散化
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
typedef __int64 LL;
#define Max 1000005
struct node
{
int num,id;
};
node a[Max];
LL c[Max];
bool cmp1(node x,node y)
{
return x.num<y.num;
}
bool cmp2(node x,node y)
{
return x.id<y.id;
}
int lowbit(int w)
{
return w&-w;
}
void add(int w,int k)
{
while(w<Max)
{
c[w]+=k;
w=w+lowbit(w);
}
}
LL sum(int n)
{
LL res=0;
while(n>0)
{
res+=c[n];
n=n-lowbit(n);
}
return res;
}
int main()
{
int i,n,j;
while(~scanf("%d",&n))
{
memset(c,0,sizeof(c));
LL res=0;
for(i=1; i<=n; i++)
{
scanf("%d",&a[i].num);
a[i].id=i;
}
sort(a+1,a+1+n,cmp1);
//离散化
for(i=1; i<=n; i++)
a[i].num=i;
sort(a+1,a+n+1,cmp2);
for(i=1; i<=n; i++)
{
add(a[i].num,1);
res=res+(i-sum(a[i].num));
}
printf("%I64d\n",res);
}
return 0;
}