树状数组
A[0]数组用来求大于k的个数
A[1]数组用来求大于x的二维逆序对个数
没遍历一个数x,ans都加上到当前位置前方大于x的二维逆序对个数
#pragma warning(disable:4996)
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn = 100005;
//树状数组
LL A[2][maxn],N,x,ans;//A树状数组
LL lowbit(LL n) { return n & -n;}
LL Get_sum(LL index,int f) {//1~index的和
LL sum = 0;
while (index)
sum += A[f][index], index -= lowbit(index);
return sum;
}
void Add(LL val, LL index,int f) {//index位置加上val
while (index < maxn)
A[f][index] += val, index += lowbit(index);
}
int main() {
#ifdef _DEBUG
freopen("in", "r", stdin);
#endif // _DEBUG
scanf("%d", &N);
for (int i = 1; i <= N;++i) {
scanf("%d", &x);
Add(1, x, 0);
Add(Get_sum(maxn-1,0)-Get_sum(x,0), x, 1);
ans += Get_sum(maxn - 1, 1)-Get_sum(x, 1);
}
cout << ans << endl;
}