Hints — p92 Count Smaller After Self
-
Reframe: for each i, count of
nums[j] < nums[i]withj > i= inversion contribution. -
BIT approach: walk i from right to left. BIT holds the multiset of values seen so far (i.e., to the right of i).
ans[i] = BIT.prefix(rank(nums[i]) - 1). ThenBIT.update(rank(nums[i]), +1). -
Coordinate compression is necessary for arbitrary-range values: map each distinct value to its rank in sorted order.
-
Merge sort alternative: during merge, track original indices. Each time you pop from left, add
right_popped_so_fartoans[original_idx]. -
Off-by-one: query
prefix(rank - 1)to count strictly smaller; usingprefix(rank)would include equal values.
If stuck: see solution.py.