<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 {
private:
    map<int,int> mp;
    vector<int> dp;
public:
    void mr(int l, int m, int r, vector<pair<int,int>> &a){
        vector<pair<int,int>> n(r-l+1);
        int idx = 0;
        int i = l;
        int j = m+1;

        //the left ptr
        int cnt=0;
        while(i <= m && j <= r){
            if(a[i].first <= a[j].first){
                dp[a[i].second] += cnt;
                n[idx] = a[i];
                i++;
            }else{
                n[idx] = a[j];
                j++;
                cnt++;
            }
                idx++;
        }

        while(i <= m){
             dp[a[i].second] += cnt;
            n[idx] = a[i];
            i++;
            idx++;
        }

        while(j <= r){
            n[idx] = a[j];
            j++;
            idx++;
        }
        idx = 0;
        for(int i = l; i<=r; i++){
            a[i] = n[idx++];
        }
    }
    void ms(vector<pair<int,int>> &nums, int l, int r){
        if(l < r){
            int m = l + (r - l) / 2;
            ms(nums, l, m);
            ms(nums, m+1, r);
            mr(l, m, r, nums);
        }
    }

    vector<int> countSmaller(vector<int>& nums) {
        vector<pair<int,int>> res;
        dp.resize(nums.size());
        //7 2 6 1
        for(int i = 0; i < nums.size(); i++){
            res.push_back({nums[i], i});
        }
        ms(res, 0, res.size()-1);

        //reverse(dp.begin(), dp.end());
        return dp;
    }
};
253143