树状数组谜之RE

P1908 逆序对

quliannanyishou @ 2022-08-14 14:15:47

#include<bits/stdc++.h>
using namespace std;
long long n,a[500010],b[500010];
long long ans1=0;
long long lowbit(long long a)
{
    return a&-a;
}
void add(long long x)
{
    for(;x<=n;x+=lowbit(x))
    {
        a[x]+=1;
    }
}
long long sum(long long x)
{
    long long ans=0;
    while(x>0)
    {
        ans+=a[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
    }
    for(int i=n;i>=1;i--)
    {
        ans1+=sum(b[i]-1);
        add(b[i]);
    }
    cout<<ans1; 
}

by 王君诺 @ 2022-08-14 14:26:23

@quliannanyishou

序列中每个数字不超过 10^9

这题需要离散化


by quliannanyishou @ 2022-08-14 14:27:49

@王君诺 我瞎了,没看见


|