求序列顺序对个数
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
#define lowbit(__a) (__a & (-__a))
typedef class IndexedTree {
public:
long long *lis;
int size;
IndexedTree() { }
IndexedTree(int size):size(size) {
lis = new long long[(size + 1)];
memset(lis, 0, sizeof(long long) * (size + 1));
}
inline void add(int idx, long long x) {
for(; idx <= size; idx += lowbit(idx))
lis[idx] += x;
}
inline long long getSum(int idx) {
long long ret = 0;
for(; idx; idx -= lowbit(idx))
ret += lis[idx];
return ret;
}
}IndexedTree;
#define ll long long
int n;
int* arr;
int* buf;
int* f;
inline void init() {
scanf("%d", &n);
arr = new int[(n + 1)];
buf = new int[(n + 1)];
f = new int[(n + 1)];
for(int i = 1; i <= n; i++)
scanf("%d", arr + i);
}
inline void discrete() {
memcpy(buf, arr, sizeof(int) * (n + 1));
sort(buf + 1, buf + n + 1);
for(int i = 1; i <= n; i++)
arr[i] = lower_bound(buf + 1, buf + n + 1, arr[i]) - buf;
}
IndexedTree it1, it2;
ll res;
inline void solve() {
it1 = IndexedTree(n);
it2 = IndexedTree(n);
for(int i = 1; i <= n; i++) {
it1.add(arr[i], 1);
f[i] = it1.getSum(arr[i] - 1);
}
for(int i = 1; i <= n; i++) {
it2.add(arr[i], f[i]);
res += it2.getSum(arr[i] - 1);
}
printf(Auto, res);
}
int main() {
init();
discrete();
solve();
return 0;
}