谁能帮我康康bug

P1908 逆序对

lemon8932 @ 2024-08-02 14:36:03

#include<bits/stdc++.h>
using namespace std;
int a[500001],t[500001];
long long ans;
void merge(int l,int r)
{
    int j,i,mid,tmp;
    mid=(l+r)/2;
    i=l;
    j=mid+1;
    tmp=l;
    while(i<=mid&&j<=r)
    {
        if(a[i]<=a[j])t[tmp++]=a[i++];
        else
        {
            t[tmp++]=a[j++];
            ans+=mid-i+1;

        }

        while(i<=mid)t[tmp++]=a[i++];
        while(j<=r)t[tmp++]=a[j++];
        for(i=l;i<=r;i++)
        {
            a[i]=t[i];
        }
    }
}
void mergesort(int l,int r)
{
    if(l<r){
    int mid;
    mid=(l+r)/2;
    mergesort(l,mid);
    mergesort(mid+1,r);
    merge(l,r);
}
}
int main()
{
    int n,i;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    mergesort(1,n);
    printf("%lld",ans);
}

|