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

void update(vector<long long> &seg_tree, long ls, long long rs, long long l, long long r, long long pos, int addV){
    if(ls<=l && rs>=r){
        if(l!=r){
            long long mid = (l+r)/2;
            update(seg_tree, ls, rs, l, mid, 2*pos+1, addV);
            update(seg_tree, ls, rs, mid+1, r, 2*pos+2, addV);
            seg_tree[pos] = seg_tree[2*pos+1] + seg_tree[2*pos+2];
        }
        else{
            seg_tree[pos] += addV;
        }
    }
    else if(ls<=r && rs>=l){
        long long mid = (l+r)/2;
        update(seg_tree, ls, rs, l, mid, 2*pos+1, addV);
        update(seg_tree, ls, rs, mid+1, r, 2*pos+2, addV);
        seg_tree[pos] = seg_tree[2*pos+1]+seg_tree[2*pos+2];
    }
    else{
        return;
    }
}
long long query(vector<long long> &seg_tree, long long ls, long long rs, long long l, long long r, long long pos){
    if(ls<=l && rs>=r){
        return seg_tree[pos];
    }
    else if(ls<=r && rs>=l){
        long long mid = (l+r)/2;
        long long a = query(seg_tree, ls, rs, l, mid, 2*pos+1);
        long long b = query(seg_tree, ls, rs, mid+1, r, 2*pos+2);
        return a+b;
    }
    else{
        return 0;
    }

}
class Solution {
public:
    int reversePairs(vector<int>& nums) {
        set<int> st;
        for(int i=0;i<nums.size();i++){
            st.insert(nums[i]);

        }
        int index = 0;
        map<long long, long long> mp;
        for(auto it = st.begin();it!=st.end();it++){
            mp[*it] = index;
            index++;
        }
        vector<long long> seg_tree(4*nums.size()+10);
        long long ans = 0;
        update(seg_tree, mp[nums[0]], mp[nums[0]], 0, index, 0, 1);
        for(int i=1;i<nums.size();i++){
            long long l = nums[i];
            auto it = mp.lower_bound(2*l+1);
            // cout<<2*l+1<<" "<<mp[l]<<endl;
            long long a;
            if(it==mp.end() || it->first<2*l){
                a = 0;
                // cout<<"NICE"<<endl;
            }
            else{
                // cout<<"it-2ndf< "<<it->second<<endl;
                long long b = query(seg_tree, it->second, it->second, 0, index, 0);
                // if(it->second==0){
                    // cout<<"LLL: "<<b<<endl;
                // }
                a = query(seg_tree, it->second, index, 0, index, 0);
            }
            cout<<"Add "<<a<<endl;
            ans+= a;
            cout<<mp[nums[i]]<<endl;
            update(seg_tree, mp[nums[i]], mp[nums[i]], 0, index, 0, 1);

            
        }
        return ans;
        
    }
};
85639