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

import collections
class Node:
    def __init__(self,start,end):
        self.start=start
        self.end=end
        self.total=0
        self.left=None
        self.right=None

class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        # construct binary tree, then perform bfs to fill in ans array 
        n=len(nums)
        ans=[0 for i in range(n)]
        sorted_nums=[]
        num_dict={} # index of nums in sorted array
        k=0
        for i in range(n): # sorted nums does not include repeated nums
            num=nums[i]
            if not num in num_dict.keys():
                num_dict[num]=0
                sorted_nums.append(num)
                k+=1

        sorted_nums.sort()

        for i in range(k):
            num_dict[sorted_nums[i]]=i

        root=Node(0,k-1)

        for i in range(k):
            cur_root=root
            while(True):
                start=cur_root.start
                end=cur_root.end
                mid=(start+end)//2
                

                if start==end and i==start:
                    break

                elif i<=mid:
                    if cur_root.left==None:
                        cur_root.left=Node(start,mid)
                    cur_root=cur_root.left

                else:
                    if cur_root.right==None:
                        cur_root.right=Node(mid+1,end)
                    cur_root=cur_root.right    

        for i in range(n-1,-1,-1):
            index=num_dict[nums[i]]
            cur_root=root
            while(True):
                mid = (cur_root.start + cur_root.end)//2
                cur_root.total+=1
                if cur_root.end==cur_root.start and cur_root.start==index:
                    break

                elif mid<index:
                    ans[i] += (cur_root.left).total
                    cur_root=cur_root.right

                else:
                    cur_root=cur_root.left

                

        return ans






        


        
       



        
38815