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

using ll = int;
class segtree {
public:
    vector<ll> seg, lazy;
    segtree(ll n) {
        seg.resize(4 * n + 1);
        lazy.resize(4 * n + 1);
    }

    // O(N)
    void build(int ind, int low, int high, vector<ll>& v) {
        if (low == high) {
            seg[ind] = v[low];
            return;
        }
        int mid = (low + high) >> 1;
        build(2 * ind + 1, low, mid, v);
        build(2 * ind + 2, mid + 1, high, v);
        seg[ind] = (seg[2 * ind + 1] + seg[2 * ind + 2]);
    }

    // O(log N)
    ll query(int ind, int low, int high, int l, int r) {
        if (low >= l && high <= r) {
            return seg[ind];
        }
        if (high < l || low > r) {
            return 0;
        }
        int mid = (low + high) >> 1;
        ll left = query(2 * ind + 1, low, mid, l, r);
        ll right = query(2 * ind + 2, mid + 1, high, l, r);
        return (left + right);
    }

    void pointUpdate(int ind, int low, int high, int node, ll val) {
        if (low == high) {
            seg[ind] += val;
        } else {
            int mid = (low + high) >> 1;
            if (node <= mid && node >= low)
                pointUpdate(2 * ind + 1, low, mid, node, val);
            else
                pointUpdate(2 * ind + 2, mid + 1, high, node, val);
            seg[ind] = (seg[2 * ind + 1] + seg[2 * ind + 2]);
        }
    }
};

class Solution {
public:
    ll low_lower_bound(vector<ll>& v, ll x) {
        ll l = 0, r = v.size() - 1;
        ll mid;
        while (r - l > 1) {
            mid = l + (r - l) / 2;
            if (v[mid] <= x) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        if (v[r] <= x)
            return r;
        if (v[l] <= x)
            return l;
        return -1;
    }

    int reversePairs(vector<int>& nums) {
        map<int, int> mp;
        for (auto ele : nums) {
            mp[ele]++;
        }
        vector<int> elements, freq;
        for (auto ele : mp) {
            elements.push_back(ele.first);
            freq.push_back(ele.second);
        }
        int n = elements.size();
        unordered_map<int, int> mpp;
        for (int i = 0; i < n; ++i) {
            mpp[elements[i]] = i;
        }
        segtree sg(n);
        sg.build(0, 0, n - 1, freq);
        int ans = 0;
        for (auto ele : nums) {
            sg.pointUpdate(0, 0, n - 1, mpp[ele], -1);
            if (ele < 0) {
                int ind = low_lower_bound(elements, ele / 2 - 1);
                if (ind == -1) {
                    continue;
                } else {
                    ans += sg.query(0, 0, n - 1, 0, ind);
                }
            } else if (ele % 2) {
                // odd
                int ind = low_lower_bound(elements, ele / 2);
                if (ind == -1) {
                    continue;
                } else {
                    ans += sg.query(0, 0, n - 1, 0, ind);
                }
            } else {
                // even
                int ind = low_lower_bound(elements, ele / 2 - 1);
                if (ind == -1) {
                    continue;
                } else {
                    ans += sg.query(0, 0, n - 1, 0, ind);
                }
            }
        }
        return ans;
    }
};
83198