<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 SegmentTree:
#     def __init__(self, val, l, r):
#         self.val = val
#         self.l = l
#         self.r = r
#         self.lc = None
#         self.rc = None

#     @staticmethod
#     def build(arr, l, r):
#         if l == r:
#             return SegmentTree(arr[l], l, r)
#         m = l + ((r - l) // 2)
#         root = SegmentTree(0, l, r)
#         root.lc = SegmentTree.build(arr, l, m)
#         root.rc = SegmentTree.build(arr, m + 1, r)
#         root.val = root.lc.val + root.rc.val
#         return root

#     def update(self, i):
#         if self.l == self.r:
#             self.val += 1
#             return
#         m = self.l + ((self.r - self.l) // 2)
#         if i > m:
#             self.rc.update(i)
#         else:
#             self.lc.update(i)
#         self.val = self.lc.val + self.rc.val

#     def query(self, l, r):
#         if self.l == l and self.r == r:
#             return self.val
#         m = self.l + ((self.r - self.l) // 2)
#         if l > m:
#             return self.rc.query(l, r)
#         elif r <= m:
#             return self.lc.query(l, r)
#         else:
#             return self.lc.query(l, m) + self.rc.query(m + 1, r)

# class Solution:
#     def countSmaller(self, nums: List[int]) -> List[int]:
#         arr = [0] * (((10 ** 4) * 2) + 1)
#         offset = 10 ** 4
#         st = SegmentTree.build(arr, 0, len(arr) - 1)
#         res = []
#         for i in range(len(nums) - 1, 0 - 1, -1):
#             res.append(st.query(0, nums[i] - 1 + offset))
#             st.update(nums[i] + offset)
#         return reversed(res)

class SegmentTree:
    def __init__(self, val, l, r):
        self.val = val
        self.l = l
        self.r = r
        self.lc = None
        self.rc = None

    @staticmethod
    def build(arr, l, r):
        if l == r:
            return SegmentTree(arr[l], l, r)
        m = l + ((r - l) // 2)
        root = SegmentTree(0, l, r)
        root.lc = SegmentTree.build(arr, l, m)
        root.rc = SegmentTree.build(arr, m + 1, r)
        root.val = root.lc.val + root.rc.val
        return root

    def update(self, i):
        if self.l == self.r:
            self.val += 1
            return
        m = self.l + ((self.r - self.l) // 2)
        if i > m:
            self.rc.update(i)
        else:
            self.lc.update(i)
        self.val = self.lc.val + self.rc.val

    def query(self, l, r):
        if l > r:
            return 0
        if self.l == l and self.r == r:
            return self.val
        m = self.l + ((self.r - self.l) // 2)
        if r <= m:
            return self.lc.query(l, r)
        elif l > m:
            return self.rc.query(l, r)
        else:
            return self.lc.query(l, m) + self.rc.query(m + 1, r)

class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        arr = [0] * (((10 ** 4) * 2) + 1)
        offset = 10 ** 4
        st = SegmentTree.build(arr, 0, len(arr) - 1)
        res = []
        for i in range(len(nums) - 1, -1, -1):
            res.append(st.query(0, nums[i] + offset - 1))
            st.update(nums[i] + offset)
        return list(reversed(res))

3968