ultimate_regular @ 2023-04-16 19:12:45
#include <bits/stdc++.h>
using namespace std;
int a[500005],b[500005];
long long ans;
void dhysb(int l,int r){
if(l==r) return ;
long long mid=l+(r-l)/2;
dhysb(l,mid);
dhysb(mid+1,r);
long long p=l,q=mid+1,t=l-1;
while(p<=mid&&q<=r){
if(a[p]<a[q]) b[++t]=a[p++];
else {
ans+=mid-p+1;
b[++t]=a[q++];
}
}
while(p<=mid) b[++t]=a[p++];
while(q<=r) b[++t]=a[q++];
for(int i=l;i<=r;i++)
a[i]=b[i];
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
dhysb(1,n);
cout<<ans;
return 0;
}
by Lovely_CCCyh___ @ 2023-04-16 19:14:46
#include<bits/stdc++.h>
using namespace std;
int n,a[500005],b[500005];
long long ans;
void msort(int l,int r)
{
if(l==r)
return;
int mid=(l+r)/2,i=l,j=mid+1,k=l;
msort(l,mid),msort(mid+1,r);
while(i<=mid&&j<=r)
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++],ans+=mid-i+1;
while(i<=mid)
b[k++]=a[i++];
while(j<=r)
b[k++]=a[j++];
for(int i=l;l<=r;l++)
a[l]=b[l];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
msort(1,n);
printf("%lld",ans);
return 0;
}
by ultimate_regular @ 2023-04-16 20:36:50
@cyh114514 感谢大佬qwq,已关注