<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 Solution {
public:
    typedef int64_t LL; 
    struct SegTree {
        int n;
        vector<LL> t;  
        SegTree (int x) {
            n = x;
            t.resize(4 * x);
        }
        LL join(LL y, LL z) {
            return y + z; //returns the sum
        }
        void modify(int v, int l, int r, int p, LL val) {
            if (l == r) {
                t[v] = val;
                return;
            }
            int m = (l + r) / 2;
            if (p <= m) {
                modify(2 * v, l, m, p, val);
            } else {
                modify(2 * v + 1, m + 1, r, p, val);
            }
            t[v] = join(t[2 * v], t[2 * v + 1]);
        }
        LL query(int v, int l, int r, int a, int b) {
            if (l > b || r < a) {
                return 0; // may need to change for max/min query
            }
            if (a <= l && r <= b) {
                return t[v];
            }
            int m = (l + r) / 2;
            return join(query(2 * v, l, m, a, b), query(2 * v + 1, m + 1, r, a, b));
        }

        // to use 
        void modify(int p, LL val) {
            modify(1, 0, n - 1, p, val);
        }
        LL query(int a, int b) {
            // care a < b
            return query(1, 0, n - 1, a, b);
        }
    };
    int reversePairs(vector<int>& nums) {
        int n = nums.size();
        vector<LL> a(n);
        map<LL, vector<LL> > m;
        SegTree seg(n);
        for(int i=0; i<n; i++){
            m[nums[i]].push_back(i);
            a[i] = nums[i];
        }
        sort(a.begin(), a.end());
        vector<LL> unique_pos;
        vector<LL> unique_neg;

        for(int i=0; i<n; i++){ 
            if(a[i] >= 0){
                if(unique_pos.size() == 0){
                    unique_pos.push_back(a[i]);
                }
                else if(unique_pos.back() != a[i]){
                    unique_pos.push_back(a[i]);
                }
            }
            else{
                if(unique_neg.size() == 0){
                    unique_neg.push_back(a[i]);
                }
                else if(unique_neg.back() != a[i]){
                    unique_neg.push_back(a[i]);
                }
            }
            
        }
        LL ans = 0;
        LL one = 0;
        for(int i=0; i<unique_neg.size(); i++){
            // for(int j=0; j<m[unique_neg[i]].size(); j++){
            //     seg.modify(m[unique_neg[i]][j], 1);
            // }
            while(one < unique_neg.size() && unique_neg[one] * 2 < unique_neg[i]){
                // cout << "ONE: " << unique_neg[i] << " " << unique_neg[one] << "\n";
                for(int j=0; j<m[unique_neg[one]].size(); j++){
                    seg.modify(m[unique_neg[one]][j], 1);
                    // cout << "MODIFY " << m[unique_neg[one]][j] << "\n";
                    // cout << "Query: " << unique_neg[one] << " " << seg.query(m[unique_neg[one]][j] + 1, n-1) << " " << ans << "\n";
                }
                one++;
            }
            vector<LL> temp = m[unique_neg[i]];
            // cout << m[unique_neg[i]].size() << ":\n";
            for(int j=0; j<temp.size(); j++){
                // cout << "query " << i << " " << temp[j] << "\n";
                if(temp[j] + 1 <= n - 1){
                    // cout << "cunt " << unique_neg[i] << " " << seg.query(temp[j] + 1, n- 1) << " " << ans << "\n";
                    ans += seg.query(temp[j] + 1, n - 1);
                }
            }
            // cout << "ONE " << unique_neg[i] << " " << unique_neg[one] << "\n";
            cout << "\n";
        }
        cout << "\n\n";
        LL ptr = 0;
        for(auto it: m){
            cout << it.first << ": ";
            LL upper = it.first * 2 + 1;
            while(ptr < unique_pos.size() && unique_pos[ptr] < upper){
                cout << "UNIQUE " << unique_pos[ptr] << " ";
                for(int i=0; i<m[unique_pos[ptr]].size(); i++){
                    ans += seg.query(m[unique_pos[ptr]][i] + 1, n-1);
                }
                ptr++;
            }
            cout << "\n";
            for(int i=0; i<it.second.size(); i++){
                cout << "IDX " << it.second[i] << " ";
                seg.modify(it.second[i], 1);
            }
            cout << "\n\n";
        }
        // cout << seg.query(1, n-1) << "\n";
        cout << ans << "\n";
        return ans;
    }
};
114934