<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 typing import List

class SegmentTree:
    def __init__(self, start, end):
        self.start = start
        self.end = end
        self.count = 0
        self.left = self.right = None

    def update(self, index, val):
        if self.start == self.end:
            self.count += val
            return

        mid = (self.start + self.end) // 2
        if index <= mid:
            if not self.left:
                self.left = SegmentTree(self.start, mid)
            self.left.update(index, val)
        else:
            if not self.right:
                self.right = SegmentTree(mid + 1, self.end)
            self.right.update(index, val)

        self.count = (self.left.count if self.left else 0) + (self.right.count if self.right else 0)

    def query(self, start, end):
        if start > self.end or end < self.start:
            return 0
        if start <= self.start and self.end <= end:
            return self.count

        left_count = self.left.query(start, end) if self.left else 0
        right_count = self.right.query(start, end) if self.right else 0
        return left_count + right_count

def print_segment_tree(node):
    def get_tree_string(node, prefix="", is_left=True):
        if node is None:
            return ""

        result = ""
        
        # Print right branch
        if node.right:
            result += get_tree_string(node.right, prefix + ("│   " if is_left else "    "), False)

        # Print current node
        result += prefix
        result += "└── " if is_left else "┌── "
        result += f"[{node.start}, {node.end}]: {node.count}"
        result += " (L)" if is_left else " (R)"
        result += "\n"

        # Print left branch
        if node.left:
            result += get_tree_string(node.left, prefix + ("    " if is_left else "│   "), True)

        return result

    print(get_tree_string(node, "", True)[:-4])  # Remove " 
    print("-" * 40)  # Separator between updates

"""
1,2,3
rank = {1:0, 2:1, 3:2}
"""
class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        if not nums: return []
        rank = {num:i for i,num in enumerate(sorted(set(nums)))}
        result, s = [], SegmentTree(0,len(rank)-1)
        for num in nums[::-1]:
            count = s.query(0, rank[num]-1) if rank[num] > 0 else 0
            result.append(count)
            s.update(rank[num],1)
        return result[::-1]



2922