P1908 逆序对 树状数组
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
#define ll long long
int a[maxn],b[maxn];
ll c[maxn];
int n;
ll ask(int x)
{
ll ans=0;
for(; x; x-=x&-x)
ans+=c[x];
return ans;
}
void add(int x,int y)
{
for(; x<=n; x+=x&-x) c[x]+=y;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int cnt=unique(b+1,b+1+n)-b-1;
ll ans=0;
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
for(int i=n; i>=1; i--)
{
ans=ans+ask(a[i]-1);
add(a[i],1);
}
printf("%lld",ans);
}