1、树状数组求逆序对
2、stable_sort的应用
3、离散化的应用
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 20;
#define ll long long
#define lowbit(x) x & -x
ll n, m, res;
struct node{
int w, id;
}a[N];
int b[N], c[N];
bool cmp(node ta, node tb){
return ta.w < tb.w;
}
ll ask(ll x){
ll ans = 0;
while(x != 0){
ans += c[x];
x -= lowbit(x);
}
return ans;
}
void add(ll x, ll y){
while(x <= n){
c[x] += y;
x += lowbit(x);
}
}
int main(){
cin >> n ;
for(int i = 1; i <= n; i++) cin >> a[i].w, a[i].id = i;
stable_sort(a + 1, a + n + 1, cmp);
for(int i = 1; i <= n; i++)
b[a[i].id] = i;
for(int i = n; i >= 1; i--){
res += ask(b[i] - 1);
add(b[i], 1);
}
cout << res << endl;
}