归并排序20个点全TLE

P1908 逆序对

cyx012113 @ 2024-02-06 15:31:36

各位大佬,我的代码是归并排序 O(n log n) ,为什么 TLE

#pragma G++ optimize(3)
#include <bits/stdc++.h>
using namespace std;

long long n,a[1000002],b[1000002],ans=0;

void msort(long long l,long long r)
{
    if (l==r) return;
    int mid=(l+r)/2;
    msort(1,mid);
    msort(mid+1,r);
    long long i=l,j=mid+1,p=l;
    while (i<=mid&&j<=r)
    {
        if (a[i]<=a[j]) b[p++]=a[i++];
        else b[p++]=a[j++],ans+=mid-i+1;
    }
    while (j<=r) b[p++]=a[j++];
    while (i<=mid) b[p++]=a[i++];
    for (long long k=l;k<=r;k++) a[k]=b[k];
}
int main()
{
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++) scanf("%lld",&a[i]);
    msort(1,n);
    printf("%lld",ans);
    //for (int i=1;i<=n;i++) printf("%d ",a[i]);
    printf("\n");
    return 0;
}

by lzm0107 @ 2024-02-06 15:36:54

@cyx012113msort(1, mid);改为msort(l, mid);


by cyx012113 @ 2024-02-06 18:31:25

@lzm0107 AC 了,感谢大佬!!!

(已加关注)


by cyx012113 @ 2024-02-06 18:34:28

@lzm0107


|