归并排序(逆序对数)
typedef long long ll; const int maxn = 1e5+500; ll qans; // 记录答案 int date[maxn]; // 数据数组 int tarray[maxn]; // 归并排序的中间数组 // POJ - 2299 SGU - 180 void merge(int arr[], int left, int right, int tarr[]) // [a, b] { 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]; }