<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]:
        
#         OFFSET = 10 ** 4
        
#         for index, each in enumerate(nums):
#             nums[index] = each + OFFSET
            
#         tree = FenwickTree((OFFSET * 2) + 2)
        
        length = len(nums)
        
#         ans = [0] * length
#         for i in range(length - 1, -1, -1):
#             ans[i] = tree.query(nums[i] - 1)
#             tree.update(nums[i], 1)
#         return ans
        
        result = [0] * length
        
        arr = [[v, i] for i, v in enumerate(nums)]  # record value and index

        
        def merge_sort(low, high):
            if low == high:
                return 
            mid = low + (high - low) // 2
            merge_sort(low, mid)
            merge_sort(mid + 1, high)
            merge(low, mid, high)
    
        def merge(low, mid, high):
            i = low
            j = mid + 1
            temp = []
            
            while i <= mid and j <= high:
                if arr[i][0] <= arr[j][0]:
                    temp.append(arr[i])
                    result[arr[i][1]] += j - (mid + 1)
                    i += 1
                else:
                    temp.append(arr[j])
                    j += 1
                    
            while i <= mid:
                temp.append(arr[i])
                result[arr[i][1]] += j - (mid + 1)
                i += 1
                
            while j <= high:
                temp.append(arr[j])
                j += 1
            
            i = low

            for each in temp:
                arr[i] = each
                i += 1
                
        merge_sort(0, length - 1)
        return result
        
                
        
        
    

class FenwickTree:
    def __init__(self, n):
        self.arr = [0] * n
        self.size = n
        
    def update(self, i, val):
        while i < self.size:
            self.arr[i] += val
            i += self.getIndex(i)
        
    def query(self, i):
        total = 0
        while i > 0:
            total += self.arr[i]
            i -= self.getIndex(i)
        return total
    
    def getIndex(self, i):
        return i & -i
    
        
        
        
50898