<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>

from collections import defaultdict

class Solution:
    def __init__(self):
        self.res = defaultdict(int)

    
    def countSmaller(self, nums: List[int]) -> List[int]:
        l = len(nums)
        nums = self.process(nums)
        # [l, r]
        self.customSort(nums, 0, len(nums)-1)

        print(self.res)

        res_array = []
        for i in range(l):
            if i in self.res:
                res_array.append(self.res[i])
            else:
                res_array.append(0)

        return res_array

    def process(self, nums):
        return [[num, i] for i, num in enumerate(nums)]

    def customSort(self, nums, l, r):
        if l >= r:
            return
        mid = l + (r-l)//2
        self.customSort(nums, l, mid)
        self.customSort(nums, mid+1, r)
        self.customMerge(nums, l, mid, mid+1, r)


    def customMerge(self, nums, l1, r1, l2, r2):
        p1, p2 = l1, l2
        right_count = 0
        lst = []
        while p1 <= r1 or p2 <= r2:
            if p1 > r1 or (p1 <= r1 and p2 <= r2 and nums[p1][0] > nums[p2][0]):
                lst.append(nums[p2][::])
                p2 += 1
                right_count += 1
            else:
                lst.append(nums[p1][::])
                self.res[nums[p1][1]] += right_count
                p1 += 1

        nums[l1: r2+1] = lst[::]
3104