归并排序(分治)
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
#define N 500010
ll a[N],b[N];
ll n,ans;
void merge_sort(ll x,ll y)
{
if (y-x>1)
{
ll m=x+(y-x)/2;
ll p=x,q=m,i=x;
merge_sort(x,m);
merge_sort(m,y);
while (p<m||q<y)
{
if (q>=y||(p<m&&a[p]<=a[q]))
b[i++]=a[p++];
else
{
if (p<m)
ans+=(m-p);
b[i++]=a[q++];
}
}
for (ll i=x;i<y;i++)
a[i]=b[i];
}
}
int main()
{
while (cin>>n&&n!=0)
{
ans=0;
for (ll i=0;i<n;i++)
cin>>a[i];
merge_sort(0,n);
cout << ans << endl;
}
return 0;
}