<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:
    class Pair:
        def __init__(self, val: int, ind: int):
            self.val = val;
            self.ind = ind;

    temp = [];
    counts = [];

    def merge(self, nums: Pair, lo: int, mid: int, hi: int):
        for pos in range(lo, hi+1):
            self.temp[pos] = nums[pos];
        i = lo;
        j = mid + 1;
        count = 0;
        for pos in range(lo, hi+1):
            if j == hi + 1:
                nums[pos] = self.temp[i];
                self.counts[self.temp[i].ind] += count;
                i += 1;
            elif i == mid + 1:
                nums[pos] = self.temp[j];
                j += 1;
            elif self.temp[i].val <= self.temp[j].val:
                nums[pos] = self.temp[i];
                self.counts[self.temp[i].ind] += count;
                i += 1;
            else:
                nums[pos] = self.temp[j];
                j += 1;
                count += 1;

    def _countSmaller(self, nums: Pair, lo: int, hi: int):
        if lo == hi:
            return;
        mid = lo + (hi - lo)//2;
        self._countSmaller(nums, lo, mid);
        self._countSmaller(nums, mid+1, hi);
        self.merge(nums, lo, mid, hi);

    def countSmaller(self, nums: List[int]) -> List[int]:
        self.counts = [0] * len(nums);
        self.temp = [self.Pair(0, 0)] * len(nums);
        arr = [];
        for i, num in enumerate(nums):
            arr.append(self.Pair(num, i));
        self._countSmaller(arr, 0, len(nums)-1);
        return self.counts;
2376