蒟蒻刚学树状数组,35分求助

P1908 逆序对

Small_Tang @ 2021-04-29 13:20:44

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define lowbit(w) w&(-w)
using namespace std;
int read()
{
    int r=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){r=r*10+ch-'0';ch=getchar();}
    return r*f;
}
int n;
LL ansl=0,s[1100000];
struct node{int x,y;}a[1100000];
bool cmp(node x,node y){return x.x>y.x;}
void change(int w)
{
    while(w<=n)
    {
        s[w]++;
        w+=lowbit(w);
    }
}
LL sum(int w)
{
    LL ans=0;
    while(w>0)
    {
        ans+=s[w];
        w-=lowbit(w);
    }
    return ans;
}
int main()
{
    freopen("1.in","r",stdin);
    n=read();
    for(int i=1;i<=n;i++){a[i].x=read();a[i].y=i;}
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        change(a[i].y);
        ansl+=sum(a[i].y-1);
    }
    printf("%lld",ansl);
    return 0;
}

by Small_Tang @ 2021-04-30 12:32:14

不用了,谢谢大家,我知道我错哪了!!!


上一页 |