归并排序,开long long还是50

P1908 逆序对

lucky_loser @ 2022-12-26 10:59:15

代码如下

#include<bits/stdc++.h>
using namespace std;
int n,a[500000],b[500000];
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 OldDriverTree @ 2022-12-26 11:00:51

@yty2011 数组越界了


by lucky_loser @ 2022-12-26 11:02:54

@guoxiangyu66 数组越界不应该是RE吗?我怎么是WA


by OldDriverTree @ 2022-12-26 11:04:11

@yty2011 有时候不一定会是RE


by w9095 @ 2022-12-26 11:04:36

@yty2011 有的时候越界并没有访问到非法内存,所以就是WA。


by lucky_loser @ 2022-12-26 11:06:19

知道了,已AC


|