Author: | Stanislav Angelyuk |
Resource: | Saratov ST team Spring Contest #1 |
Date: | 18.05.2003 |
/**************************** * author:crazy_石头 * date:2014/01/15 * algorithm:BIT * Pro:SGU180-inversions ***************************/ #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <map> #include <string> using namespace std; #define INF 1<<29 #define eps 1e-8 #define A system("pause") #define rep(i,h,n) for(int i=(h);i<=(n);i++) #define ms(a,b) memset((a),(b),sizeof(a)) #define LL __int64 const int maxn=70000+10; struct Node { LL id,num; }e[maxn]; LL C[maxn],n,h[maxn]; inline bool cmp(Node E,Node D) { return E.num<D.num; } inline int lowbit(int x) { return x&-x; } inline void update(int x,int d) { while(x<=n) { C[x]+=d; x+=lowbit(x); } } inline LL getsum(int x) { LL ret=0; while(x>0) { ret+=C[x]; x-=lowbit(x); } return ret; } int main() { while(~scanf("%d",&n)) { ms(C,0); rep(i,1,n) { scanf("%I64d",&e[i].num); e[i].id=i; } sort(e+1,e+n+1,cmp); h[e[1].id]=1; int cnt=1; rep(i,2,n) { if(e[i].num!=e[i-1].num) cnt++; h[e[i].id]=cnt; } LL sum=0; rep(i,1,n) { update(h[i],1); sum+=i-getsum(h[i]); } printf("%I64d\n",sum); } return 0; }