逆序对(归并
luogu P1908
归并排序做法:(树状数组明天康康
每次归并记录逆序对
int yuan[500010],m[500010];
long long ans=0;
void solve(int s,int t)
{
if(s==t)
return;
int mid=s+t>>1;
solve(s,mid),solve(mid+1,t);
int i=s,j=mid+1,k=s;
while(i<=mid&&j<=t)
{
if(yuan[i]<=yuan[j])
m[k++]=yuan[i++];
else
{
m[k++]=yuan[j++];
ans+=mid-i+1;
}
}
while(i<=mid)
m[k++]=yuan[i++];
while(j<=t)
m[k++]=yuan[j++];
for(i=s;i<=t;i++)
yuan[i]=m[i];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
yuan[i]=read();
}
solve(1,n);
printf("%lld\n",ans);
return 0;
}