# 计算右侧小于当前元素的个数

<p>给你`一个整数数组 <code>nums</code><em> </em>，按要求返回一个新数组&nbsp;<code>counts</code><em> </em>。数组 <code>counts</code> 有该性质： <code>counts[i]</code> 的值是&nbsp; <code>nums[i]</code> 右侧小于&nbsp;<code>nums[i]</code> 的元素的数量。</p>

<p>&nbsp;</p>

<p><strong>示例 1：</strong></p>

<pre>
<strong>输入：</strong>nums = [5,2,6,1]
<strong>输出：</strong><code>[2,1,1,0] 
<strong>解释：</strong></code>
5 的右侧有 <strong>2 </strong>个更小的元素 (2 和 1)
2 的右侧仅有 <strong>1 </strong>个更小的元素 (1)
6 的右侧有 <strong>1 </strong>个更小的元素 (1)
1 的右侧有 <strong>0 </strong>个更小的元素
</pre>

<p><strong>示例 2：</strong></p>

<pre>
<strong>输入：</strong>nums = [-1]
<strong>输出：</strong>[0]
</pre>

<p><strong>示例 3：</strong></p>

<pre>
<strong>输入：</strong>nums = [-1,-1]
<strong>输出：</strong>[0,0]
</pre>

<p>&nbsp;</p>

<p><strong>提示：</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>


## template

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    struct BSTNode
    {
        int val;
        int count;
        BSTNode *left;
        BSTNode *right;
        BSTNode(int x) : val(x), left(NULL), right(NULL), count(0) {}
    };

    void BST_insert(BSTNode *node, BSTNode *insert_node, int &count_small)
    {
        if (insert_node->val <= node->val)
        {
            node->count++;
            if (node->left)
            {
                BST_insert(node->left, insert_node, count_small);
            }
            else
            {
                node->left = insert_node;
            }
        }
        else
        {
            count_small += node->count + 1;
            if (node->right)
            {
                BST_insert(node->right, insert_node, count_small);
            }
            else
            {
                node->right = insert_node;
            }
        }
    }

    vector<int> countSmaller(vector<int> &nums)
    {
        vector<int> result;
        vector<BSTNode *> node_vec;
        vector<int> count;
        for (int i = nums.size() - 1; i >= 0; i--)
        {
            node_vec.push_back(new BSTNode(nums[i]));
        }
        count.push_back(0);
        for (int i = 1; i < node_vec.size(); i++)
        {
            int count_small = 0;
            BST_insert(node_vec[0], node_vec[i], count_small);
            count.push_back(count_small);
        }
        for (int i = node_vec.size() - 1; i >= 0; i--)
        {
            delete node_vec[i];
            result.push_back(count[i]);
        }
        return result;
    }
};


```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```