求逆序对个数
1 ll a[amn],tmpA[amn],cnt=0; 2 void merge_sort(ll l, ll r, ll *A) { 3 if (l >= r) return ; 4 int mid = (l + r) >> 1; 5 merge_sort(l, mid, A); 6 merge_sort(mid + 1, r, A); 7 int pl = l, pr = mid + 1, tmpp = 0; 8 while(pl <= mid && pr <= r) { 9 if (A[pl] <= A[pr]) tmpA[tmpp++] = A[pl++]; 10 else tmpA[tmpp++] = A[pr++], cnt += mid - pl + 1; 11 } 12 while(pl <= mid) tmpA[tmpp++] = A[pl++]; 13 while(pr <= r) tmpA[tmpp++] = A[pr++]; 14 for (int i = 0; i < tmpp; i++) A[i + l] = tmpA[i]; 15 }
cnt 保存逆序对个数