<p>Given an integer array <code>nums</code>, return<em> an integer array </em><code>counts</code><em> where </em><code>counts[i]</code><em> is the number of smaller elements to the right of </em><code>nums[i]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,6,1]
<strong>Output:</strong> [2,1,1,0]
<strong>Explanation:</strong>
To the right of 5 there are <b>2</b> smaller elements (2 and 1).
To the right of 2 there is only <b>1</b> smaller element (1).
To the right of 6 there is <b>1</b> smaller element (1).
To the right of 1 there is <b>0</b> smaller element.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1]
<strong>Output:</strong> [0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-1]
<strong>Output:</strong> [0,0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

// class Solution {
//     void merge(vector<int>& nums, vector<int>& inx, int s, int mid, int e, vector<int>& res){
//         int i = s, j = mid + 1, inversions = 0;
//         vector<int> tmp;

//         while (i <= mid || j <= e) { // merge operation
//             if (i <= mid && (j > e || nums[inx[i]] <= nums[inx[j]])) {
//             res[inx[i]] += inversions;
//             tmp.push_back(inx[i++]);
//             }
//             else {
//             tmp.push_back(inx[j++]);
//             ++inversions;
//             }
//         }
//         move(tmp.begin(), tmp.end(), inx.begin() + s);
//     }

//     void mergeSort(vector<int>& nums, vector<int>& inx, int s, int e, vector<int>& res) {
//         if (s >= e) return;

//         int mid = s + (e - s) / 2;
        
//         mergeSort(nums, inx, s, mid, res);
//         mergeSort(nums, inx, mid+1, e, res);

//         merge(nums, inx, s, mid, e, res);
//     }
// public:
//     vector<int> countSmaller(vector<int>& nums) {
//         vector<int> res(nums.size()), inx(nums.size());
//         iota(inx.begin(), inx.end(), 0);
//         mergeSort(nums, inx, 0, nums.size() - 1, res);
//         return res;
//     }
// };
class Solution {
    void merge(vector<pair<int,int>>& nums, int s, int mid, int e, vector<int>& res){
        int i = s, j = mid + 1, inversions = 0;
        vector<pair<int,int>> tmp;

        while (i <= mid || j <= e) { // merge operation
            if (i <= mid && (j > e || nums[i].first <= nums[j].first)) {
            res[nums[i].second] += inversions;
            tmp.push_back(nums[i++]);
            }
            else {
            tmp.push_back(nums[j++]);
            ++inversions;
            }
        }
        move(tmp.begin(), tmp.end(), nums.begin() + s);
    }

    void mergeSort(vector<pair<int,int>>& nums, int s, int e, vector<int>& res) {
        if (s >= e) return;

        int mid = s + (e - s) / 2;
        
        mergeSort(nums, s, mid, res);
        mergeSort(nums, mid+1, e, res);

        merge(nums, s, mid, e, res);
    }
public:
    vector<int> countSmaller(vector<int>& nums) {
        vector<int> res(nums.size());
        vector<pair<int,int>> v;
        for(int i=0;i<nums.size();i++){
            v.push_back({nums[i],i});
        }
        mergeSort(v, 0, nums.size() - 1, res);
        return res;
    }
};
432918