Process from right, maintaining sorted structure. Count elements smaller than current.
It works because as you add elements from right to left, the sorted structure tells you how many existing elements are smaller.
Example: [5,2,6,1]. Process : smaller. Process : smaller. Process : smaller. Process : smaller. Result: [2,1,1,0].
Segment tree or merge sort. For : time. Merge sort: operations. space.