逆序对模板(树状数组)
//P1908 逆序对 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int mxn=5e5+5; int a[mxn],c[mxn],b[mxn],f[mxn]; int n; inline int lowbit(int x){ return x&-x; } inline ll get(int x){ ll sum=0; for(;x;x-=lowbit(x)){ sum+=c[x]; } return sum; } inline void motify(int x,int y){ for(;x<=n;x+=lowbit(x)){ c[x]+=y; } } int main(){ cin>>n; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); int N=unique(b+1,b+n+1)-b-1; for(int i=1;i<=n;i++){ a[i]=lower_bound(b+1,b+N+1,a[i])-b; } for(int i=1;i<=n;i++){ motify(a[i],1); f[i]=i-get(a[i]); } ll ans=0; for(int i=1;i<=n;i++){ ans+=f[i]; } cout<<ans; return 0; }