<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 Node{
    public:
    Node()
    {
        this->l = 0;
        this->r = 0;
        this->arr = vector<pair<int,int>>();
    }

    Node(int l, int r)
    {
        this->l = l;
        this->r = r;
        this->arr.assign(r-l+1,{0,0});
    }

    int l;
    int r;
    vector<pair<int,int>> arr;
};

class SegTree{
    vector<Node> t;
    unordered_map<int,int> mp;
    vector<int> counts;
    public:

    SegTree(int n, vector<int>& nums)
    {
        t.assign(2*n, Node());
        mp.clear();
        build(0, 0, n-1, nums);

        counts = vector<int>();
        for(int i = 0; i<n; i++)
        {
            counts.push_back(mp[i]);
        }
    }

    void build(int i, int l, int r, vector<int>& nums)
    {
        if(l > r)
        {
            return;
        }

        if(l == r)
        {
            t[i] = Node(l,r);
            t[i].arr[0].first = nums[l];
            t[i].arr[0].second = l;
            return;
        }

        int mid = l + (r-l)/2;
        t[i] = Node(l, r);
        build(i+1, l, mid, nums);
        build(i+2*(mid-l+1), mid+1, r, nums);
        merge(t[i], t[i+1], t[i+2*(mid-l+1)]);
    }

    void merge(Node& t1, Node& t2, Node& t3)
    {
        int k = 0;
        int i = 0;
        int j = 0;

        int n = t2.arr.size();
        int m = t3.arr.size();

        int count = 0;

        while(i < n && j < m)
        {
            if(t2.arr[i].first <= t3.arr[j].first)
            {
                t1.arr[k] = t2.arr[i];
                mp[t2.arr[i].second]+=count;
                k++;
                i++;
            }
            else
            {
                count++;
                t1.arr[k] = t3.arr[j];
                k++;
                j++;
            }
        }

        while(i < n)
        {
            t1.arr[k] = t2.arr[i];
            mp[t2.arr[i].second]+=count;
            k++;
            i++;
        }

        while(j < m)
        {
            t1.arr[k] = t3.arr[j];
            k++;
            j++;
        }
    }

    vector<int> ans()
    {
        return counts;
    }
};

class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        SegTree st(n, nums);
        return st.ans();
    }
};
395906