归并排序求逆序对
#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
const int M = 2001;
typedef long long LL;
pair<LL, LL> p[N];
LL a[N], b[N];
LL fun(LL l, LL r)
{
//LL w = 0;
if (l >= r) return 0;
LL mid = (l + r) >> 1;
LL w = fun(l ,mid) + fun(mid + 1, r);
LL i = l, j = mid + 1, k = 0;
while(i <= mid && j <= r)
{
if (a[i] <= a[j]) b[k++] = a[i++];
else
{
w += mid - i + 1;
b[k++] = a[j++];
}
}
while (i <= mid) b[k++] = a[i++];
while (j <= r) b[k++] = a[j++];
for (LL o = 0; o < k; ++o)
{
a[o + l] = b[o];
}
return w;
}
int main()
{
LL n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
LL w = fun(0, n - 1);
//for (int i = 0; i < n; ++i) cout << a[i] << ' ';
//cout << endl;
cout << w << endl;
return 0;
}