<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= indexDiff &lt;= nums.length</code></li>
	<li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li>
</ul>

class TreeNode:
    def __init__(self, val: int) -> TreeNode:
        self.parent = None
        self.left_child = None
        self.right_child = None
        self.val = val
        self.rank = 0
    
    def add_node(self, node: TreeNode) -> None:
        self.rank += 1
        if self.val < node.val:
            if self.right_child is None:
                self.right_child = node
                node.parent = self
            else:
                self.right_child.add_node(node)
        else:
            if self.left_child is None:
                self.left_child = node
                node.parent = self
            else:
                self.left_child.add_node(node)
    
    def derank(self) -> None:
        self.rank -= 1
        if self.parent:
            self.parent.derank()

    def remove_from_tree(self) -> None:
        move_up = None
        if self.left_child and self.right_child:
            if self.left_child.rank > self.right_child.rank:
                move_up = self.left_child
                move_up.add_node(self.right_child)
            else:
                move_up = self.right_child
                move_up.add_node(self.left_child)
        elif self.left_child:
            move_up = self.left_child
        elif self.right_child:
            move_up = self.right_child

        if self.parent.left_child == self:
            self.parent.left_child = move_up
        else:
            self.parent.right_child = move_up
        if move_up:
            move_up.parent = self.parent
        self.derank()

    def get_in_range(self, val: int, v_diff: int) -> bool:
        if abs(val - self.val) <= v_diff:
            return True
        if self.right_child and (not self.left_child or self.left_child.val < val):
            if self.right_child.get_in_range(val, v_diff):
                return True
        if self.left_child and (not self.right_child or val < self.right_child.val):
            if self.left_child.get_in_range(val, v_diff):
                return True
        return False

    def print_tree(self, res: List[int]) -> None:
        if self.left_child:
            self.left_child.print_tree(res)
        res.append(self.val)
        if self.right_child:
            self.right_child.print_tree(res)

import sortedcontainers

class Solution:
    def containsNearbyAlmostDuplicate(self, nums: List[int], i_diff: int, v_diff: int) -> bool:
        return self._sol2(nums, i_diff, v_diff)

    def _sol2(self, nums: List[int], i_diff: int, v_diff: int) -> bool:
        window = sortedcontainers.SortedDict({nums[0] : 0})
        for i in range(1, len(nums)):
            val = nums[i]
            if i > i_diff:
                del window[nums[i - i_diff - 1]]
            # Binary search through the keys
            key_view = window.keys()
            low = 0
            high = len(key_view) - 1
            while True:
                j = (low + high) // 2
                if abs(val - key_view[j]) <= v_diff:
                    return True
                if val < key_view[j]:
                    if j == low:
                        break
                    high = j - 1
                else:
                    if j == high:
                        break
                    low = j + 1
            window[val] = i
        return False

    # Perf: time limit exceeded on 48 / 52. Interesting project though.
    def _bst(self, nums: List[int], i_diff: int, v_diff: int) -> bool:
        root = TreeNode(math.inf)
        nodes_by_index = []
        for i, val in enumerate(nums):
            if i > i_diff:
                nodes_by_index[i - i_diff - 1].remove_from_tree()
            if root.get_in_range(val, v_diff):
                return True
            i_node = TreeNode(val)
            nodes_by_index.append(i_node)
            root.add_node(i_node)
        return False
            
        
34825