<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 Solution {
public:

    class orderedSet{
        public:
        struct Node{
            int count; // number of elements in this node[Left, Right]
            Node* left, *right;
            Node(){
                count = 0;
                left = nullptr, right = nullptr;
            }
        }; 
        Node* update(Node* node, int l, int r, int val, int add){
            if(node == nullptr){
                node = new Node();
            }
            if(l == r){
                node->count = node->count + add;
                return node;
            }
            int mid = (l+r)/2;
            if(val <= mid)
                node->left = update(node->left, l, mid, val, add);
            else 
                node->right = update(node->right, mid+1, r, val, add);
            node->count = ( node->left != NULL? node->left->count : 0) + 
            (node->right != NULL ? node->right->count : 0);
            return node;
        }
        int query(Node*node, int l, int r, int val){
            if(!node or l >= val){
                return 0;
            }
            if(r < val){
                return node->count;
            }
            int mid = (l+r)/2, count = 0;
            return query(node->left, l, mid, val) + query(node->right, mid+1, r, val);
        }
        int L, R;
        Node* root;
        orderedSet(int N){
            L = -N, R = N;
            root = new Node();
        }
        void insert(int val){
            update(root, L, R, val, 1);
        }
        void remove(int val){
            update(root, L, R, val, -1);
        }
        int count(int val){
            return query(root, L, R, val);
        }
    };

    vector<int> countSmaller(vector<int>& a) {
        int limit = 2e4 + 50;
        int halfLimit = 1e4+5;
        orderedSet s(limit);
        int n = a.size();
        vector<int> ans(n, 0);

        for(int i = n-1; i>=0; i--){
            s.insert(a[i] + halfLimit);
            ans[i] = s.count(a[i]+halfLimit);
        }
        return ans;
    }
};
126243