一半AC,一半WA

P1908 逆序对

Liuyc1127 @ 2023-12-06 16:24:12

#include<bits/stdc++.h>
using namespace std;
int ans=0,cnt,sum[500009<<5],ls[500009<<5],rs[500009<<5],a[500009],rt;
void updata(int &p,int l,int r,int k) {
    if(p==0)
        p=++cnt;
    sum[p]++;
    if(l==r)
        return ;
    int mid=l+r>>1;
    if(k<=mid)
        updata(ls[p],l,mid,k);
    else
        updata(rs[p],mid+1,r,k);    
    return ;    
}
int check(int p,int l,int r,int k) {
    if(l==r)
        return 0;
    int mid=l+r>>1;
    if(k<=mid)
        return sum[rs[p]]+check(ls[p],l,mid,k);
    else
        return check(rs[p],mid+1,r,k);
}
signed main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    for(int i=1; i<=n; i++) {
        updata(rt,-1,1e9+3,a[i]);
        ans=ans+check(1,-1,1e9+3,a[i]);
    }
    cout<<ans<<"\n";
    return 0;
}

by Terrible @ 2023-12-06 16:27:15

@Liuyc1127

仅仅你开的 4 个全局数组就有

(500009\times 4\times 97)/1024/1024\approx 185.0161476135254 \mathrm{MB}

by Terrible @ 2023-12-06 16:29:05

ans 怎么是 int?运算溢出了吧。


by Liuyc1127 @ 2023-12-06 16:30:24

@Terrible 谢谢!!


by Liuyc1127 @ 2023-12-06 16:31:00

@Terrible 关注了


|