#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; ll sum[500050],n; struct node{ ll lf; ll rg; }a[500050]; ll cmp(node x,node y) { if(x.lf==y.lf) return x.rg<y.rg; return x.lf<y.lf; } ll lowbit(ll x) { return x&(-x); } ll Point_change(ll x) { while(x<=n) { sum[x]=sum[x]+1; x=x+lowbit(x); } } ll get_sum(ll x) { ll res=0; while(x>0) { res=res+sum[x]; x=x-lowbit(x); } return res; } int main() { ll sm; while(scanf("%lld",&n)) { if(n==0) break; memset(sum,0,sizeof(sum)); for(ll i=1;i<=n;i++) { scanf("%lld",&a[i].lf); a[i].rg=i; } sort(a+1,a+n+1,cmp); sm=0; for(ll i=1;i<=n;i++) { ll st=get_sum(a[i].rg); ll ed=get_sum(n); Point_change(a[i].rg); sm=sm+ed-st; } printf("%lld\n",sm); } }
来源:https://www.cnblogs.com/Leozi/p/10835105.html