<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>

#define ll long long
#define MAX_VAL 20001 // [-10^4, 10^4] shifted by +10^4 to handle negatives

class SegmentTree {
public:
    ll size;
    vector<ll> segmentTree;

    // Build the tree with all 0 initially
    SegmentTree(ll size) {
        this->size = size;
        segmentTree = vector<ll>(4 * size, 0);
    }

    // Point update to add a value
    void update(int nodeIndex, int left, int right, int idx, ll value) {
        if (left == right) {
            segmentTree[nodeIndex] += value;
            return;
        }
        int mid = (left + right) / 2;
        if (idx <= mid)
            update(2 * nodeIndex + 1, left, mid, idx, value);
        else
            update(2 * nodeIndex + 2, mid + 1, right, idx, value);
        segmentTree[nodeIndex] = segmentTree[2 * nodeIndex + 1] + segmentTree[2 * nodeIndex + 2];
    }

    void update(ll idx, ll value) {
        update(0, 0, size - 1, idx, value);
    }

    // Query the number of elements in the range [0, queryRight]
    ll query(int nodeIndex, int left, int right, int queryLeft, int queryRight) {
        if (queryRight < left || right < queryLeft) return 0; // No overlap
        if (queryLeft <= left && right <= queryRight) return segmentTree[nodeIndex]; // Full overlap
        int mid = (left + right) / 2;
        return query(2 * nodeIndex + 1, left, mid, queryLeft, queryRight) +
               query(2 * nodeIndex + 2, mid + 1, right, queryLeft, queryRight);
    }

    ll query(ll left, ll right) {
        return query(0, 0, size - 1, left, right);
    }
};


class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        vector<int> result(n);

        SegmentTree segTree(MAX_VAL);

        for (int i = n - 1; i >= 0; --i) {
            int shiftedValue = nums[i] + 10000;
            result[i] = segTree.query(0, shiftedValue - 1);
            segTree.update(shiftedValue, 1);
        }

        return result;
    }
};
131531