<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 TreeNode:
    def __init__(self, val, left = None, right = None):
        self.val = val
        self.left = left
        self.right = right
        self.height = 1
        self.count = 1

class AVLTree:
    def __init__(self):
        self.root = None
    
    def add_value(self, value):
        self.root = self.add(self.root, value)

    def add(self, root, value):
        if not root:
            return TreeNode(value)
        
        if root.val == value:
            root.count += 1
            return root

        if value < root.val:
            root.left = self.add(root.left, value)
        else:
            root.count += 1
            root.right = self.add(root.right, value)
        self.adjust_height(root)

        bal = self.balance(root)
        if bal > 1:
            if value > root.left.val:
                root.left = self.left_rotate(root.left)
            root = self.right_rotate(root)
        elif bal < -1:
            if value < root.right.val:
                root.right = self.right_rotate(root.right)
            root = self.left_rotate(root)

        return root

    def height(self, root):
        return root.height if root else 0
        
    def adjust_height(self, root):
        root.height = 1 + max(self.height(root.left), self.height(root.right))

    def balance(self, root):
        return self.height(root.left) - self.height(root.right) if root else 0

    def left_rotate(self, pivot):
        pivot_next = pivot.right
        pivot_next.left, pivot.right = pivot, pivot_next.left
        self.adjust_height(pivot)
        self.adjust_height(pivot_next)
        pivot.count -= pivot_next.count
        return pivot_next

    def right_rotate(self, pivot):
        pivot_next = pivot.left
        pivot_next.right, pivot.left = pivot, pivot_next.right
        self.adjust_height(pivot)
        self.adjust_height(pivot_next)
        pivot_next.count += pivot.count
        return pivot_next

    def find_value(self, value):
        return self.find(self.root, value)

    def find(self, root, value):
        if not root:
            return 0
        if root.val == value:
            return root.count
        elif root.val < value:
            return self.find(root.right, value)
        else:
            return root.count + self.find(root.left, value)

class Solution:
    def reversePairs(self, nums: List[int]) -> int:
        avl = AVLTree()
        res = 0
        for num in nums:
            res += avl.find_value(2 * num + 1)
            avl.add_value(num)
        return res

27200