我用的树状数组+离散化
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define lowbit(x) x&(-x)
using namespace std;
const int N = 5e5 + 10;
int n;
int a[N];
int d[N];
struct Pro
{
int val, id;
}z[N];
bool cmp(Pro a, Pro b)
{
return a.val < b.val;
}
void add(int x, int v)
{
while(x <= N)
{
d[x] += v;
x += lowbit(x);
}
}
long long query(int x)
{
long long res = 0;
while(x)
{
res += d[x];
x -= lowbit(x);
}
return res;
}
void Disc()
{
sort(z + 1, z + 1 + n, cmp);
for(int i = 1; i <= n; i ++)
a[z[i].id] = i;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> z[i].val, z[i].id = i;
Disc();
long long ans = 0;
for(int i = 1; i <= n; i ++)
{
ans += query(n) - query(a[i]);
add(a[i], 1);
}
cout << ans << endl;
return 0;
}