AcWing 788. 逆序对的数量
原题链接
简单
作者:
走不到也得走
,
2020-01-11 20:40:41
,
所有人可见
,
阅读 593
#include<iostream>
using namespace std;
const int N=1e6+10;
typedef long long ll;
int q[N],tmp[N];
int n;
ll merge(int l,int r)
{
if(l>=r)return 0;
int mid=l+r>>1;
ll res=merge(l,mid)+merge(mid+1,r);
int k=0,i=l,j=mid+1;
while(i<=mid&&j<=r)
{
if(q[i]<=q[j])tmp[k++]=q[i++];
else{
tmp[k++]=q[j++];
res+=mid-i+1;
}
}
while(i<=mid)tmp[k++]=q[i++];
while(j<=r)tmp[k++]=q[j++];
for(i=l,j=0;i<=r;i++,j++)q[i]=tmp[j];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&q[i]);
printf("%lld",merge(0,n-1));
//cout<<merge(0,n-1)<<endl;
return 0;
}