#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int N=5e5+10; int n; int a[N]; int tr[N]; int b[N]; int c[N]; int lowbit(int x) { return x&-x; } void add(int x,int c) { for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=c; } int sum(int x) { int res=0; for(int i=x;i;i-=lowbit(i)) res+=tr[i]; return res; } int main() { while(cin>>n,n) { for(int i=1;i<=n;i++) { cin>>a[i]; b[i]=A [I]; } // end position Sort (b + . 1 , b + . 1 + n-); // A positions of the elements in the b, c, then placed // A 1 ~ n elements should be in a position // equivalent discretized for ( int I = . 1 ; I <= n-; I ++ ) C [I] = lower_bound (B + . 1 , B + . 1 + n-, A [I]) - B; Memset (TR, 0 , the sizeof TR); LL ANS = 0 ; for ( int I = . 1 ; I <= n-; I ++) { add(c[i],1); ans+=i-sum(c[i]); } cout<<ans<<endl; } }