<p>Given a <strong>(0-indexed)</strong> integer array <code>nums</code> and two integers <code>low</code> and <code>high</code>, return <em>the number of <strong>nice pairs</strong></em>.</p>

<p>A <strong>nice pair</strong> is a pair <code>(i, j)</code> where <code>0 &lt;= i &lt; j &lt; nums.length</code> and <code>low &lt;= (nums[i] XOR nums[j]) &lt;= high</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,4,2,7], low = 2, high = 6
<strong>Output:</strong> 6
<strong>Explanation:</strong> All nice pairs (i, j) are as follows:
    - (0, 1): nums[0] XOR nums[1] = 5 
    - (0, 2): nums[0] XOR nums[2] = 3
    - (0, 3): nums[0] XOR nums[3] = 6
    - (1, 2): nums[1] XOR nums[2] = 6
    - (1, 3): nums[1] XOR nums[3] = 3
    - (2, 3): nums[2] XOR nums[3] = 5
</pre>

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

<pre>
<strong>Input:</strong> nums = [9,8,4,2,1], low = 5, high = 14
<strong>Output:</strong> 8
<strong>Explanation:</strong> All nice pairs (i, j) are as follows:
​​​​​    - (0, 2): nums[0] XOR nums[2] = 13
&nbsp;   - (0, 3): nums[0] XOR nums[3] = 11
&nbsp;   - (0, 4): nums[0] XOR nums[4] = 8
&nbsp;   - (1, 2): nums[1] XOR nums[2] = 12
&nbsp;   - (1, 3): nums[1] XOR nums[3] = 10
&nbsp;   - (1, 4): nums[1] XOR nums[4] = 9
&nbsp;   - (2, 3): nums[2] XOR nums[3] = 6
&nbsp;   - (2, 4): nums[2] XOR nums[4] = 5</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= low &lt;= high &lt;= 2 * 10<sup>4</sup></code></li>
</ul>
class TrieNode:
    def __init__(self, zero: Optional["TrieNode"] = None, one: Optional["TrieNode"] = None):
        self.zero = zero
        self.one = one
        self.count = 0

class Trie:
    def __init__(self):
        self.root = TrieNode()
        self.binary_length = 15
    
    def add(self, val: int) -> None:
        cur = self.root
        for d in self._to_binary(val):
            if d == "1":
                if not cur.one:
                    cur.one = TrieNode()
                cur = cur.one
            else:
                if not cur.zero:
                    cur.zero = TrieNode()
                cur = cur.zero
            cur.count += 1
    
    def count(self, val: int, target: int) -> int:
        count = 0
        b1 = self._to_binary(val)
        b2 = self._to_binary(target)
        cur = self.root
        for d1, d2 in zip(b1, b2):
            if not cur:
                break
            if d2 == "1":
                if d1 == "1":
                    count += cur.one.count if cur.one else 0
                    cur = cur.zero
                else:
                    count += cur.zero.count if cur.zero else 0
                    cur = cur.one
            else:
                if d1 == "1":
                    cur = cur.one
                else:
                    cur = cur.zero
        count += cur.count if cur else 0
        return count

    def _to_binary(self, val: int) -> str:
        b = bin(val)[2:]
        return "0" * (self.binary_length - len(b)) + b

class Solution:
    def countPairs(self, nums: List[int], low: int, high: int) -> int:
        trie = Trie()
        count = 0
        for num in nums:
            count += trie.count(num, high) - trie.count(num, low - 1)
            trie.add(num)
        return count

22200