class Solution { public: int InversePairs(vector<int> data) { if(data.empty()) return 0; int n=data.size(); vector<int> copy(n); return Inverse(data,copy,0,n-1); } int Inverse(vector<int> &data,vector<int> ©,int start,int end) { if(start==end) { return 0; } int mid=(start+end)>>1; int left=Inverse(data,copy,start,mid); int right=Inverse(data,copy,mid+1,end); int count=0; int ll=mid; int rr=end; int k=end; while(ll>=start&&rr>mid) { if(data[ll]>data[rr]) { count+=rr-mid; copy[k--]=data[ll--]; } else { copy[k--]=data[rr--]; } } while(ll>=start) copy[k--]=data[ll--]; while(rr>mid) copy[k--]=data[rr--]; for(int i=start;i<=end;i++) data[i]=copy[i]; return left+right+count; } };