代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define LL long long
const int M = 1e6+5;
using namespace std;
LL c[M], sum[M], a[M], b[M], c2[M];
LL bit(LL x){
	return x & -x;
}
void add(LL x){
	while(x < M){
		c[x]++;
		x += bit(x);
	}
}
LL getsum(LL x){
	LL sum = 0;
	while(x){
		sum += c[x];
		x -= bit(x);
	}
	return sum;
}
int main(){
	LL n, i;
	cin >> n;
	for(i = 1; i < M; ++ i) sum[i] = sum[i-1]+i;
	for(i = 0; i < n; ++ i){  //当前点跟左边点形成的逆序对数 
		cin >> a[i];  //确保每个点都被更新
		add(a[i]+1);
		b[i] = i+1-getsum(a[i]+1);
	}
	memset(c, 0, sizeof(c));
	LL res = 0;
	for(int i = n-1; i >= 0; -- i){ //当前点跟右边点形成的逆序对数 
		add(a[i]+1);
		res += sum[b[i]+getsum(a[i])];		
	}
	cout <<res <<endl;
	return 0;
} 
原文:http://blog.csdn.net/shengweisong/article/details/44464769