<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 countSmaller(self, nums: List[int]) -> List[int]:
        
        class Node:
            value=0
            index=0
            length=0
            def __init__(self,val,ind):
                self.value=val
                self.index=ind
        
        def merge(brr,s1,e1,s2,e2):
            new = []
            nonlocal arr
            l=0
            t1=s1
            # print(arr1,arr2,s1,e1,s2,e2)
            while s1<=e1 and s2<=e2:
                if arr[s1].value<=arr[s2].value:
                    arr[s1].length+=l
                    new.append(arr[s1])
                    s1+=1
                else:
                    l+=1
                    new.append(arr[s2])
                    s2+=1
            while s1<=e1:
                arr[s1].length+=l;
                new.append(arr[s1])
                s1+=1
                
            while s2<=e2:
                new.append(arr[s2])
                s2+=1
            for i in range(t1,e2+1):
                arr[i]=new[i-t1]
            return
        
        def mergeSort(nums,start,end):
            
            if start>=end:
                return
            mid=(start+end)//2
            
            mergeSort(nums,start,mid)
            mergeSort(nums,mid+1,end)
            merge(nums,start,mid,mid+1,end)
            
            return
        
        arr=[]
        for i in range(len(nums)):
            arr.append(Node(nums[i],i))
        # print(arr)
        mergeSort(arr,0,len(arr)-1)
        brr=arr
        ans=[0 for i in range(len(nums))]
        for i in range(len(brr)):
            ans[brr[i].index]=brr[i].length
        return ans
        
52688