逆序对:a[i]>a[j]&&i<j
Adjacent bubble sort number for the number of times the sequence is switched to the reverse
One:
By merge sort
Long Long CNT = 0 ; void Merge ( int L, int R & lt) { IF (L> = R & lt) return ; int MID = (L + R & lt) >> . 1 ; Merge (L, MID), Merge (MID + . 1 , R & lt); int I = L, J = MID + . 1 , K = 0 ; the while (I <= MID && J <= R & lt) IF (A [I] <= A [J]) B [K ++] = A [I ++ ]; the else B [K ++] = a [J ++], CNT + = MID-I + . 1 ; // when a [i]> a [j ] i <j and i ~ mid j ~ r is a sequence of two ordered, then a [i ~ mid] may be configured to reverse the A [J] the while(i<=mid)b[k++]=a[i++]; while(j<=r)b[k++]=a[j++]; for(int i=0,j=l;j<=r;j++,i++) a[j]=b[i]; return ; }
two;
By Fenwick tree