50分树状数组查错

P1908 逆序对

墨希 @ 2020-11-01 18:49:39

RT

Code:

#include<iostream>  
#include<stdio.h>
#include<algorithm>
using namespace std;
const int M=500005;
int n,m,ans,a[M],b[M],c[M],x[M];
inline void read(int &v)
{
    int rec=0,flag=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')  flag=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')  rec=rec*10+ch-'0',ch=getchar();
    v=flag*rec;
return;
}
int lowbit(int x){ return x&-x; }
int ask(int x)
{
    int rec=0;
    for(;x;x-=lowbit(x))  rec+=c[x];
return rec;
}
void add(int x)
{
    for(;x<=n;x+=lowbit(x))  c[x]++;
return;
}
int main()  
{  
    read(n);
    for(int i=1;i<=n;i++)  read(a[i]),x[i]=a[i];
    sort(x+1,x+1+n);
    m=unique(x+1,x+1+n)-(x+1);
    for(int i=1;i<=n;i++)  b[i]=lower_bound(x+1,x+1+m,a[i])-x;
    for(int i=n;i>=1;i--)
    {
        ans+=ask(b[i]-1);
        add(b[i]);
    }
    printf("%d",ans);
return 0;
}

by dead_X @ 2020-11-01 18:52:11

bklljzz


by guodong @ 2020-11-01 19:10:54

@dead_X sxsdmzl


by 墨希 @ 2020-11-01 19:14:22

啊我发现了。。。

我没开long long (0_0)


|