<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:
  # // segment tree with lazy propagation for range max query
  # // key: index (used to locate children), value: stats for this range, or this subtree, in this example, it's range max
  # idea: each booking can be considered as + 1 booking for every time point in the range
  # Space: O(n), where n is the number of bookings
  def __init__(self, min_val, max_val, arr=None):
    self.tree = defaultdict(int)
    self.lower = min_val
    self.upper = max_val
    if arr is not None:
      self.build(arr)

  def merge_fn(self, child1, child2):
    # // DIFF 1: replace "max" with your merge fn.
    return child1 + child2

  def query(self, ql, qh, cl=None, ch=None, index=1):
    if cl is None: cl = self.lower
    if ch is None: ch = self.upper
    # // cl, ch are the range of the current tree node. index is the current tree node index.
    # // ql, qh are the range to be queried.
    if ch < ql or cl > qh:
      # // DIFF 2: for different problems, the default value can be different.
      # // E.g., for min-range, return MAX.
      return 0
    if ql <= cl and ch <= qh:
      return self.tree[index]

    m = (cl + ch) // 2
    return self.merge_fn(
      self.query(ql, qh, cl, m, 2 * index),
      self.query(ql, qh, m + 1, ch, 2 * index + 1)
    )

  # Update a range without lazy propagation.
  def update(self, ql, qh, cl=None, ch=None, index=1):
    if cl is None: cl = self.lower
    if ch is None: ch = self.upper
    # // cl, ch are the range of the current tree node. index is the current tree node index.
    # // ql, qh are the range to be updated.
    if ch < ql or cl > qh:
      return
    if cl == ch:
      # // DIFF 3: update how the value should be updated.
      self.tree[index] += 1
      return

    m = (cl + ch) // 2
    self.update(ql, qh, cl, m, 2 * index)
    self.update(ql, qh, m + 1, ch, 2 * index + 1)
    self.tree[index] = self.merge_fn(self.tree[2 * index], self.tree[2 * index + 1])
    return

  def update_single(self, qp, cl=None, ch=None, index=1):
    if cl is None: cl = self.lower
    if ch is None: ch = self.upper
    # // cl, ch are the range of the current tree node. index is the current tree node index.
    # // qp is the query point to be updated.
    if ch < qp or cl > qp:
      return
    if cl == ch:
      # // DIFF 4: update how the value should be updated.
      self.tree[index] += 1
      return

    m = (cl + ch) // 2

    if qp <= m:
      self.update_single(qp, cl, m, 2 * index)
    elif m + 1 <= qp:
      # // Note: we use `m+1` instead of `m`. Otherwise we will recurse infinitely.
      self.update_single(qp, m + 1, ch, 2 * index + 1)

    self.tree[index] = self.merge_fn(self.tree[2 * index], self.tree[2 * index + 1])
    return

  # `arr` maps query point to its value. It might be better to use a dict if the init values are sparse.
  # build() can only be called once after init. Otherwise call update instead.
  def build(self, arr: List[int], cl=None, ch=None, index=1):
    if cl is None: cl = self.lower
    if ch is None: ch = self.upper
    # // cl, ch are the range of the current tree node. index is the current tree node index.
    if cl == ch:
      if cl >= 0 and cl < len(arr):
        self.tree[index] = arr[cl]
      return

    m = (cl + ch) // 2
    self.build(arr, cl, m, 2 * index)
    self.build(arr, m + 1, ch, 2 * index + 1)
    self.tree[index] = self.merge_fn(self.tree[2 * index], self.tree[2 * index + 1])
    return

class Solution:
  def countSmaller(self, nums: List[int]) -> List[int]:
    tree = SegmentTree(min(nums), max(nums))
    if not nums:
      return []
    res = []
    for i in range(len(nums) - 1, -1, -1):
      res.append(tree.query(tree.lower, nums[i] - 1))
      tree.update_single(nums[i])
    return list(reversed(res))
4241