const int maxn=5e5+7;
int a[maxn],b[maxn];//原数组,辅助数组;
ll change(int l,int mid,int r){
int i=l,j=mid+1;
ll res=0;
int k=0;
while(i<=mid&&j<=r)
if(a[i]<=a[j]) b[k++]=a[i++];
else{
/*
*在集合[l,mid]与[mid+1,r]中的元素各自有序,
*故若a[j]<a[i],则说明从[i,mid]这一段序列的元素都大于a[j],因此逆序对数就是这段序列长度;
*/
res+=mid-i+1;
b[k++]=a[j++];
}
while(i<=mid) b[k++]=a[i++];
while(j<=r) b[k++]=a[j++];
for(i=0;i<k;++i) a[i+l]=b[i];
return res;
}
ll merage_sort(int l,int r){
if(l>=r) return 0;
ll res=0;
int mid=(l+r)>>1;
res+=merage_sort(l,mid);
res+=merage_sort(mid+1,r);
res+=change(l,mid,r);
return res;
}