康复训练
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int tmp[N], q[N];
long ans = 0;
void f(int l, int r) {
if (l >= r) return;
int mid = (l + r) >> 1;
f(l, mid), f(mid + 1, r);
int i = l, j = mid + 1, k = 0;
while (i <= mid && j <= r) {
if (q[j] < q[i]) {
ans += mid - i + 1;
tmp[k++] = q[j++];
} else tmp[k++] = q[i++];
}
while (i <= mid) tmp[k++] = q[i++];
while (j <= r) tmp[k++] = q[j++];
for (i = 0, j = l; j <= r; ++i, ++j) q[j] = tmp[i];
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> q[i];
f(1, n);
cout << ans << endl;
return 0;
}