<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:
    def reversePairs(self, nums: List[int]) -> int:
        # based on merge sort
        
        def _merge_sort(res, arr, left, right):
            # right is not included
            if right - left >= 2:
                mid = left + (right - left) // 2
                res, arr = _merge_sort(res, arr, left, mid)
                res, arr = _merge_sort(res, arr, mid, right)
                res, arr = _merge(res, arr, left, mid, right)
            return res, arr
        
        def _merge(res, arr, left, mid, right):
            # arr: each element is a tuple as (val, index)
            i = left
            j = mid
            p = mid  # NOTE: this is very important! We use two pointers to process sorted array and reverse pairs separately
            sorted_arr = []

            # print('before: ', [_[0] for _ in arr[left:mid]], [_[0] for _ in arr[mid:right]], res)

            # we must process all i first
            while i < mid:
                while (p < right) and (arr[i][0] > 2 * arr[p][0]):
                    p += 1
                res[arr[i][1]] += p - mid

                while (j < right) and (arr[i][0] >= arr[j][0]):
                    sorted_arr.append(arr[j])
                    j += 1
                
                sorted_arr.append(arr[i])
                i += 1
            
            while j < right:
                sorted_arr.append(arr[j])
                j += 1
                
            # print('after: ', [_[0] for _ in arr[left:mid]], [_[0] for _ in arr[mid:right]], [_[0] for _ in sorted_arr], res)
            
            for tmp_i in range(left, right):
                arr[tmp_i] = sorted_arr[tmp_i - left]

            return res, arr
            
        arr = []
        res = []
        for tmp_i, tmp in enumerate(nums):
            arr.append((tmp, tmp_i))
            res.append(0)
        
        # print('\nbefore: ', [_[0] for _ in arr])
        res, arr = _merge_sort(res, arr, 0, len(nums))
        # print('\nafter: ', [_[0] for _ in arr])

        # print('\nres: ', res)

        return sum(res)



# def _wrong_merge(res, arr, left, mid, right):
#     # arr: each element is a tuple as (val, index)
#     i = left
#     j = mid
#     p = mid
#     sorted_arr = []

#     print('before: ', [_[0] for _ in arr[left:mid]], [_[0] for _ in arr[mid:right]], res)

#     while (i < mid) and (j < right):
#         print(i, j, left, mid, right)
#         if arr[i][0] <= 2 * arr[j][0]:
#             res[arr[i][1]] += j - mid
#             # process elements from j to right
#             while (j < right) and (arr[i][0] >= arr[j][0]):
#                 sorted_arr.append(arr[j])
#                 j += 1
#             sorted_arr.append(arr[i])
#             i += 1
#         else:
#             p = j
#             sorted_arr.append(arr[j])
#             j += 1

#     while i < mid:
#         if arr[i][0] > 2 * arr[p][0]:
#             res[arr[i][1]] += p - mid + 1
#         sorted_arr.append(arr[i])
#         i += 1
    
#     while j < right:
#         sorted_arr.append(arr[j])
#         j += 1
    
#     print('after: ', [_[0] for _ in arr[left:mid]], [_[0] for _ in arr[mid:right]], res)
    
#     for tmp_i in range(left, right):
#         arr[tmp_i] = sorted_arr[tmp_i - left]

#     return res, arr
27600