<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:
    def __merge(self, low: int, mid: int, high: int, arr: list[tuple[int, int]], res: list[int]) -> None:
        temp: list[int] = list()
        p1, p2, points = low, mid+1, 0

        while p1 <= mid and p2 <= high:
            n1, i = arr[p1]
            n2, j = arr[p2]

            if n1 > n2:
                points += 1
                temp.append((n2, j))
                p2 += 1
            else:
                res[i] += points
                temp.append((n1, i))
                p1 += 1
        
        while p1 <= mid:
            res[arr[p1][1]] += points
            temp.append(arr[p1])
            p1 += 1
        
        while p2 <= high:
            temp.append(arr[p2])
            p2 += 1
        
        for i in range(low, high+1):
            arr[i] = temp[i-low]

    def __mergeSort(self, low: int, high: int, arr: list[tuple[int, int]], res: list[int]) -> None: 
        if low == high: return
        mid = (high - low) // 2 + low 
        self.__mergeSort(low, mid, arr, res)
        self.__mergeSort(mid+1, high, arr, res)
        self.__merge(low, mid, high, arr, res)

    def countSmaller(self, nums: List[int]) -> List[int]:
        n: int = len(nums)
        res: list[int] = [0]*n
        arr: list[tuple[int, int]] = [(num, i) for i, num in enumerate(nums)]
        self.__mergeSort(0, n-1, arr, res)

        return res
2149