<p>Given an integer array <code>nums</code>, return <em>the number of <strong>reverse pairs</strong> in the array</em>.</p>

<p>A <strong>reverse pair</strong> is a pair <code>(i, j)</code> where:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt; nums.length</code> and</li>
	<li><code>nums[i] &gt; 2 * nums[j]</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,2,3,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The reverse pairs are:
(1, 4) --&gt; nums[1] = 3, nums[4] = 1, 3 &gt; 2 * 1
(3, 4) --&gt; nums[3] = 3, nums[4] = 1, 3 &gt; 2 * 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,3,5,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The reverse pairs are:
(1, 4) --&gt; nums[1] = 4, nums[4] = 1, 4 &gt; 2 * 1
(2, 4) --&gt; nums[2] = 3, nums[4] = 1, 3 &gt; 2 * 1
(3, 4) --&gt; nums[3] = 5, nums[4] = 1, 5 &gt; 2 * 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class SegmentTree {
public:
    vector<int> seg;
    SegmentTree(int n) {
        seg.resize(4LL * n, 0);
    }
    void update(int ind, int left, int right, int i) {
        if(left == right) {
            seg[ind] += 1;
            return;
        }
        int mid = left + (right - left) / 2;
        if(i <= mid) {
            update(2 * ind + 1, left, mid, i);
        } else {
            update(2 * ind + 2, mid + 1, right, i);
        }
        seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
    }
    int query(int ind, int left, int right, int l, int r) {
        // complete overlap [l left right r]
        if(l <= left and r >= right) {
            return seg[ind];
        }
        // no overlap [l r left right]
        if(r < left or l > right) {
            return 0;
        }
        int mid = left + (right - left) / 2;
        int count = 0;
        count += query(2 * ind + 1, left, mid, l, r);
        count += query(2 * ind + 2, mid + 1, right, l, r);
        return count;
    }
};
struct BSTreeNode {
    int value;
    int leftCount;
    int rightCount;
    BSTreeNode* left;
    BSTreeNode* right;
    BSTreeNode(int v) {
        value = v;
        leftCount = 0;
        rightCount = 0;
        left = NULL;
        right = NULL;
    }
};
class Solution {
public:
    BSTreeNode* InsertNodeInTree(BSTreeNode* root, int value) {
        if(root == NULL) {
            return new BSTreeNode(value);
        }
        if(value >= root->value) {
            root->rightCount += 1;
            root->right = InsertNodeInTree(root->right, value);
        } else {
            root->leftCount += 1;
            root->left = InsertNodeInTree(root->left, value);
        }
        return root;
    }
    int SearchInTree(BSTreeNode* root, int value) {
        if(root == NULL) {
            return 0;
        }
        if(root->value > (long long)2 * value) {
            return 1 + root->rightCount + SearchInTree(root->left, value);
        }
        return SearchInTree(root->right, value);
    }
    void merge(vector<int>& nums, int left, int mid, int right, vector<int>& temp, int& count) {
        int i = left;
        int j = mid + 1;
        int k = 0;
        // while(i <= mid and j <= right) {
        //     if(nums[i] > (long long)2 * nums[j]) {
        //         i++;
        //     } else {
        //         count += (i - left);
        //         j++;
        //     }
        // }
        // if(i > mid) count += (i - left) * (right - mid - (j - 1 - mid));
        while(i <= mid and j <= right) {
            if(nums[i] > (long long)2 * nums[j]) {
                count += (mid - i + 1);
                j += 1;
            } else {
                i += 1;
            }
        }
        i = left;
        j = mid + 1;
        while(i <= mid and j <= right) {
            if(nums[i] <= nums[j]) {
                temp[k++] = nums[i++];
            } else {
                temp[k++] = nums[j++];
            }
        }
        while(i <= mid) {
            temp[k++] = nums[i++];
        }
        while(j <= right) {
            temp[k++] = nums[j++];
        }
        for(int i = left; i <= right; i++) {
            nums[i] = temp[i - left];
        }
    }
    void mergeSort(vector<int>& nums, int left, int right, vector<int>& temp, int& count) {
        if(left < right) {
            int mid = left + (right - left) / 2;
            mergeSort(nums, left, mid, temp, count);
            mergeSort(nums, mid + 1, right, temp, count);
            merge(nums, left, mid, right, temp, count);
        }
    }
    int mergeSortSolution(vector<int>& nums) {
        vector<int> temp(nums.size());
        int count = 0;
        mergeSort(nums, 0, nums.size() - 1, temp, count);
        return count;
    }
    int bstSolution(vector<int>& nums) {
        BSTreeNode* root = InsertNodeInTree(NULL, nums[0]);
        int count = 0;
        for(int j = 1; j < nums.size(); j++) {
            count += SearchInTree(root, nums[j]);
            root = InsertNodeInTree(root, nums[j]);
        }
        return count;
    }
    int segmentTreeSolution(vector<int>& nums) {
        set<int> s;
        int maxValue = nums[0];
        for(int n : nums) {
            s.insert(n);
            maxValue = max(maxValue, n);
        }
        int idx = 0;
        map<long long,int> rankMap;
        for(int n : s) {
            rankMap[n] = idx++;
        }
        SegmentTree st(idx);
        int count = 0;
        for(int j = 0; j < nums.size(); j++) {
            if(2LL * nums[j] + 1 > maxValue) {
                st.update(0, 0, idx, rankMap[nums[j]]);
                continue;
            }
            auto it = rankMap.lower_bound(2LL * nums[j] + 1);
            // cout << j << " " << it->first << "\n";
            // if(it != rankMap.begin()) {
            //     it--;
            // }
            count += st.query(0, 0, idx, it->second, idx);
            // cout << j << " " << it->second << " " << count << "\n";
            st.update(0, 0, idx, rankMap[nums[j]]);
        }
        return count;
    }
    int reversePairs(vector<int>& nums) {
        // return bstSolution(nums);
        return segmentTreeSolution(nums);
    }
};
75874