<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]:
        # implement segment tree
        def update(index, value, tree, size):
            index += size  # shift the index to the leaf
            # update from leaf to root
            tree[index] += value
            while index > 1:
                index //= 2
                tree[index] = tree[index * 2] + tree[index * 2 + 1]

        def query(left, right, tree, size):
            # return sum of [left, right)
            result = 0
            left += size  # shift the index to the leaf
            right += size
            while left < right:
                # if left is a right node
                # bring the value and move to parent's right node
                if left % 2 == 1:
                    result += tree[left]
                    left += 1
                # else directly move to parent
                left //= 2
                # if right is a right node
                # bring the value of the left node and move to parent
                if right % 2 == 1:
                    right -= 1
                    result += tree[right]
                # else directly move to parent
                right //= 2
            return result

        offset = 10**4  # offset negative to non-negative
        size = 2 * 10**4 + 1  # total possible values in nums
        #why can be 2*size
        tree = [0] * (4 * size)
        result = []
        for num in reversed(nums):
            smaller_count = query(0, num + offset, tree, size)
            result.append(smaller_count)
            update(num + offset, 1, tree, size)
        return reversed(result)



class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        m = len(nums)
        tree = {}
        ans = []
        def update(index, lo, hi,i,val):
            #i will be equal to lo and hi as well
            if lo==hi:
                # print(f'i:{i} lo;{lo} hi:{hi}')
                if index not in tree:
                    tree[index]=1
                else:
                    tree[index]+=val
                return
            mid = (lo+hi)//2
            if i>mid:
                update(2*index+2, mid+1,hi,i,val)
            elif i<=mid:
                update(2*index+1,lo,mid,i,val)
            tree[index]=(tree[2*index+1] if (2*index+1) in tree else 0)\
            +(tree[2*index+2] if (2*index+2) in tree else 0)
        def query(index, lo, hi, i,j):
            if lo>=i and hi<=j:
                return tree[index] if index in tree else 0
            if hi<i or lo>j:
                return 0
            mid = (lo+hi)//2
            lSum = query(2*index+1,lo,mid,i,j)
            rSum = query(2*index+2,mid+1,hi,i,j)
            return lSum+rSum
        
        for i in range(m-1,-1,-1):
            v = nums[i]
            r = query(0,-10000,10000,-10000,v-1)
            ans.append(r)
            # print(f'v:{v} r:{r}')
            update(0,-10000,10000,v,1)
            # print(tree)



        return reversed(ans)

            

class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        m = len(nums)
        minV = min(nums)
        maxV = max(nums)
        ans = []
        offset = abs(minV) if minV<0 else 0
        size = maxV+offset+1
        tree = [0]*(4*size)

        def update(index, lo, hi,i,val):
            #i will be equal to lo and hi as well
            if lo==hi:
                # print(f'i:{i} lo;{lo} hi:{hi} index:{index}')
                tree[index]+=val
                return
            mid = (lo+hi)//2
            if i>mid:
                update(2*index+2, mid+1,hi,i,val)
            elif i<=mid:
                update(2*index+1,lo,mid,i,val)
            tree[index]=tree[2*index+1] +tree[2*index+2]
        def query(index, lo, hi, i,j):
            if lo>=i and hi<=j:
                return tree[index]
            if hi<i or lo>j:
                return 0
            mid = (lo+hi)//2
            lSum = query(2*index+1,lo,mid,i,j)
            rSum = query(2*index+2,mid+1,hi,i,j)
            return lSum+rSum
        
        for i in range(m-1,-1,-1):
            v = nums[i]
            vOffset = v+offset
            r = query(0,0,size,0,vOffset-1)
            ans.append(r)
            # print(f'v:{v} r:{r}')
            update(0,0,size,vOffset,1)
            # print(tree)



        return reversed(ans)
3332