This is a common solution with a vector storing the sorted elements on the right of the target. It has O(n^2) time in the worst case because the insertion takes O(n) time in the worst case. I just leverage some C++ APIs to make the solution shorter.

```
vector<int> countSmaller(vector<int>& nums) {
vector<int> result(nums.size()), sorted;
for (int i = nums.size() - 1; i >= 0; --i) {
auto it = sorted.insert(lower_bound(sorted.begin(), sorted.end(), nums[i]), nums[i]);
result[i] = it - sorted.begin();
}
return result;
}
```