<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>Find the longest subsequence of <code>nums</code> that meets the following requirements:</p>

<ul>
	<li>The subsequence is <strong>strictly increasing</strong> and</li>
	<li>The difference between adjacent elements in the subsequence is <strong>at most</strong> <code>k</code>.</li>
</ul>

<p>Return<em> the length of the <strong>longest</strong> <strong>subsequence</strong> that meets the requirements.</em></p>

<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>

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

<pre>
<strong>Input:</strong> nums = [4,2,1,4,3,4,5,8,15], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong>
The longest subsequence that meets the requirements is [1,3,4,5,8].
The subsequence has a length of 5, so we return 5.
Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.
</pre>

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

<pre>
<strong>Input:</strong> nums = [7,4,5,1,8,12,4,7], k = 5
<strong>Output:</strong> 4
<strong>Explanation:</strong>
The longest subsequence that meets the requirements is [4,5,8,12].
The subsequence has a length of 4, so we return 4.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,5], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The longest subsequence that meets the requirements is [1].
The subsequence has a length of 1, so we return 1.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li>
</ul>

class SegTree:
    def __init__(self, arr: List[int], l: int, r: int):
        self.l = l
        self.r = r
        if l == r:
            self.maxValue = arr[l]
        else:
            mid = (l + r) // 2
            self.left = SegTree(arr, l, mid)
            self.right = SegTree(arr, mid+1, r)
            self.maxValue = max(self.left.maxValue, self.right.maxValue)
    
    def getMaxValue(self, l: int, r: int):
        if l <= self.l and self.r <= r:
            return self.maxValue
        elif l > self.r or r < self.l:
            return 0
        else:
            return max(self.left.getMaxValue(l, r), self.right.getMaxValue(l, r))
    
    def updateValue(self, idx: int, val: int):
        if idx == self.l == self.r:
            self.maxValue = val
        elif idx > self.r or idx < self.l:
            return
        else:
            self.left.updateValue(idx, val)
            self.right.updateValue(idx, val)
            self.maxValue = max(self.left.maxValue, self.right.maxValue)

class Solution:
    def lengthOfLIS(self, nums: List[int], k: int) -> int:
        dp = [0] * (max(nums) + 1)
        root = SegTree(dp, 0, len(dp) - 1)

        for i in range(len(nums)):
            rmin = max(nums[i] - k, 0)
            rmax = nums[i] - 1
            maxVal = root.getMaxValue(rmin, rmax)
            root.updateValue(nums[i], maxVal + 1)

        return root.getMaxValue(0, len(dp) - 1)
57200