50分求助,归并排序,TLE后面十个点

P1908 逆序对

csbe @ 2021-07-15 16:59:51

#include<cstdio>
using namespace std;
long long ans=0;
int a[500010],b[500010],n;
void mergesort(int l,int r){
    if(l==r)return;
    int mid=(l+r)>>1;
    mergesort(l,mid);
    mergesort(mid+1,r);
    int p1=l,p2=mid+1,p3=l;
    while(p1<=mid&&p2<=r){
        if(a[p1]<=a[p2])b[p3]=a[p1],p1++,p3++;
        else b[p3]=a[p2],p3++,p2++,ans+=mid-p1+1;   
    }
    while(p1<=mid)b[p3]=a[p1],p3++,p1++;
    while(p2<=r)b[p3]=a[p2],p3++,p2++;
    for(int i=1;i<=r;i++)a[i]=b[i];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    mergesort(1,n);
    printf("%d",ans);
    return 0;
}

by Uzumaki @ 2021-07-15 17:23:01

for(int i=1;i<=r;i++)a[i]=b[i];

1 改成 l


by csbe @ 2021-07-16 14:39:00

@陈宣羽 谢谢大佬


|