O(nlogn)
在归并过程中得到答案
ll merge_sort(int a[], int l, int r)
{
if (l >= r)
{
return 0;
}
int mid = l + r >> 1;
ll cnt = merge_sort(a, l, mid) + merge_sort(a, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r)
{
if (a[i] <= a[j])
{
tmp[k++] = a[i++];
}
else
{
tmp[k++] = a[j++];
cnt += mid - i + 1;
}
}
while (i <= mid)
{
tmp[k++] = a[i++];
}
while (j <= r)
{
tmp[k++] = a[j++];
}
for (i = l, j = 0; i <= r;)
{
a[i++] = tmp[j++];
}
return cnt;
}