// language C with STL(C++)
// 剑指51
// https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/
class Solution {
public:
int core(vector<int>& nums,int low,int high){
if(high-low <=0)
return 0;
int mid = (low + high)/2;
int res = core(nums, low,mid) +core(nums, mid+1, high);
int i = low, j = mid +1-low, k = low;
int fuzhu[high-low+1]; // 0号对应着low号
for(;i<=high; i++){
fuzhu[i-low] = nums[i];
}
for(i=0; i<mid+1-low && j<=high-low;k++){
if(fuzhu[i]<=fuzhu[j]){
nums[k] = fuzhu[i];
i++;
}else{
nums[k] = fuzhu[j];
j++;
res += mid+1-low-i;
}
}
while(i<mid+1-low){
nums[k++] = fuzhu[i++];
}
while(j<=high-low){
nums[k++] = fuzhu[j++];
}
return res;
}
int reversePairs(vector<int>& nums) {
return core(nums,0, nums.size()-1);
}
};