1 void merge_sort(int l,int r) { 2 if (l == r) { 3 return; 4 } 5 int mid = l + r >> 1; 6 merge_sort(l, mid); 7 merge_sort(mid + 1, r); 8 int i = l, j = mid + 1, k = l; 9 while (i <= mid && j <= r) { 10 if (a[i] <= a[j]) { 11 b[k++] = a[i++]; 12 } else { 13 ans+=mid-i+1; 14 b[k++] = a[j++]; 15 } 16 } 17 while (i <= mid) { 18 b[k++] = a[i++]; 19 } 20 while (j <= mid) { 21 b[k++] = a[j++]; 22 } 23 for (int i = l; i <= r; i++) { 24 a[i] = b[i]; 25 } 26 }