<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):
        self.n = 2 * 10 ** 4 + 1
        self.offset = 10 ** 4
        self.arr = [0] * 2 * self.n

    def add(self, value):
        idx = self.n + value + self.offset
        self.arr[idx] += 1
        p = idx >> 1
        while p > 0:
            self.arr[p] = self.arr[p << 1] + self.arr[(p << 1) | 1]
            p >>= 1

    def query(self, value):
        l, r = self.n, self.n + value + self.offset
        count = 0
        while l < r:
            if l & 1:
                count += self.arr[l]
                l += 1
            if r & 1:
                r -= 1
                count += self.arr[r]
            l >>= 1
            r >>= 1
        return count


class Solution:
    # SegmentTree
    def countSmaller(self, nums: List[int]) -> List[int]:
        n = len(nums)
        count = [0] * n
        st = SegmentTree()
        for i in reversed(range(n)):
            count[i] = st.query(nums[i])
            st.add(nums[i])
        return count

    # Merge sort
    # def countSmaller(self, nums: List[int]) -> List[int]:
    #     n = len(nums)
    #     count = [0] * n
    #     # num -> index
    #     pairs = [(num, i) for i, num in enumerate(nums)]
    #     mergeSort(pairs, count, 0, n - 1)
    #     return count


# Sort in desc order counting inverisons for each element
def mergeSort(pairs, inversions, l, r):
    if r - l < 0:
        return []
    elif r - l == 0:
        # single element
        return [pairs[l]]
    mid = l + (r - l) // 2
    left = mergeSort(pairs, inversions, l, mid)
    right = mergeSort(pairs, inversions, mid + 1, r)
    return merge(inversions, left, right)

def merge(inversions, left, right):
    res = []
    i, j = 0, 0
    m, n = len(left), len(right)
    while i < m and j < n:
        lval, lidx = left[i]
        rval, ridx = right[j]
        if lval <= rval:
            res.append(right[j])
            j += 1
        else:
            inversions[lidx] += n - j
            res.append(left[i])
            i += 1
    while i < m:
        res.append(left[i])
        i += 1
    while j < n:
        res.append(right[j])
        j += 1
    return res

2285