<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of size <code>n</code>, and an integer <code>diff</code>. Find the number of <strong>pairs</strong> <code>(i, j)</code> such that:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt;= n - 1</code> <strong>and</strong></li>
	<li><code>nums1[i] - nums1[j] &lt;= nums2[i] - nums2[j] + diff</code>.</li>
</ul>

<p>Return<em> the <strong>number of pairs</strong> that satisfy the conditions.</em></p>

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

<pre>
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong>
There are 3 pairs that satisfy the conditions:
1. i = 0, j = 1: 3 - 2 &lt;= 2 - 2 + 1. Since i &lt; j and 1 &lt;= 1, this pair satisfies the conditions.
2. i = 0, j = 2: 3 - 5 &lt;= 2 - 1 + 1. Since i &lt; j and -2 &lt;= 2, this pair satisfies the conditions.
3. i = 1, j = 2: 2 - 5 &lt;= 2 - 1 + 1. Since i &lt; j and -3 &lt;= 2, this pair satisfies the conditions.
Therefore, we return 3.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [3,-1], nums2 = [-2,2], diff = -1
<strong>Output:</strong> 0
<strong>Explanation:</strong>
Since there does not exist any pair that satisfies the conditions, we return 0.
</pre>

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

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= diff &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def numberOfPairs(self, A: List[int], B: List[int], diff: int) -> int:
        return UsingSegmentTree().numberOfPairs(A, B, diff)

class UsingSegmentTree:
    def numberOfPairs(self, A: List[int], B: List[int], diff: int) -> int:
        SHIFT = 30001
        n = 50001
        for i in range(len(A)):
            A[i] = A[i] - B[i] + SHIFT
        segmentTree = [0] * (n * 4 + 1)
        count = 0
        for j in A:
            count += self.query(segmentTree, 1, 1, n, 1, j + diff)
            self.update(segmentTree, 1, 1, n, j)
        return count

    def update(self, segmentTree: List[int], index: int, start: int, end: int, updateIndex: int) -> None:
        if start == end:
            segmentTree[index] += 1
            return
        mid = start + (end - start) // 2
        if updateIndex <= mid:
            self.update(segmentTree, index * 2, start, mid, updateIndex)
        else:
            self.update(segmentTree, index * 2 + 1, mid + 1, end, updateIndex)
        segmentTree[index] = segmentTree[index * 2] + segmentTree[index * 2 + 1]

    def query(self, segmentTree: List[int], index: int, start: int, end: int, queryStart: int, queryEnd: int) -> int:
        if end < start or start > queryEnd:
            return 0
        if start >= queryStart and end <= queryEnd:
            return segmentTree[index]
        mid = start + (end - start) // 2
        left = self.query(segmentTree, index * 2, start, mid, queryStart, queryEnd)
        right = self.query(segmentTree, index * 2 + 1, mid + 1, end, queryStart, queryEnd)
        return left + right
35700