ll qans; // 记录答案 int date[maxn]; // 数据数组 int tarray[maxn]; // 归并排序的中间数组 // POJ - 2299 SGU - 180 void merge(int arr[], int left, int right, int tarr[]) { if (left>=right) return ; int m = (left+right) >> 1; merge(arr, left, m, tarr); merge(arr, m+1, right, tarr); int i,j, cnt; i = left; j = m+1; cnt = 0; while (i<=m && j<=right) { if (arr[i] <= arr[j]) tarr[cnt++] = arr[i++]; else { qans += (ll)m - i + 1; // 求逆序对. tarr[cnt++] = arr[j++]; } } while (i<=m) tarr[cnt++] = arr[i++]; while (j<=right) tarr[cnt++] = arr[j++]; for (i=0; i<cnt; ++i) arr[left++] = tarr[i]; }
原文:https://www.cnblogs.com/cgjh/p/9347674.html