全RE……求dalao

P1908 逆序对

xiao_qiu @ 2024-07-03 16:12:05

#include<bits/stdc++.h>
#define inx 500010
using namespace std;
long long e[inx],n,a[inx],ans;
void fix(long long x)
{
    for(long long i=x;i<=inx;i+=i&-i) e[i]++;
}
long long getsum(long long x)
{
    long long ret=0;
    for(long long i=x;i;i-=i&-i) ret+=e[i];
    return ret;
 } 
int main()
{
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(long long i=n;i;i--)
    {
        ans+=getsum(a[i]-1);
        fix(a[i]);
    }
    printf("%lld",ans);
    return 0;
}

by zhangbo1000 @ 2024-07-03 16:14:28

@xiao_qiu e[inx] 访问越界


by StarsIntoSea_SY @ 2024-07-06 11:22:09

@xiao_qiu

第 7 行改成:

for(long long i=x;i<=n;i+=i&-i) e[i]++;

by xiao_qiu @ 2024-07-19 10:36:00

谢谢大佬们!


by xiao_qiu @ 2024-07-19 15:51:32

dalao们我这个又调了一下…… 还是全RE……麻烦再帮我看一下

#include<bits/stdc++.h>
#define inx 500010
using namespace std;
int e[inx],n,a[inx];
long long ans;
void fix(int x)
{
    for(int i=x;i<=n;i+=i&-i) e[i]++;
}
long long getsum(int x)
{
    long long ret=0;
    for(int i=x;i;i-=i&-i) ret+=e[i];
    return ret;
 } 
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(int i=n;i;i--)
    {
        ans+=getsum(a[i]-1);
        fix(a[i]);
    }
    printf("%lld",ans);
    return 0;
}

|