<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>

from bisect import bisect_left

class Node:
    def __init__(self, L, R, nums):
        self.L = L
        self.R = R
        if L == R:
            self.total = nums[L]
            self.left = None
            self.right = None
            return

        M = (L + R) // 2
        self.left = Node(L, M, nums)
        self.right = Node(M + 1, R, nums)
        self.total = max(self.left.total, self.right.total)
    
    def update(self, index, val):
        if index not in range(self.L, self.R + 1):
            return
        
        M = (self.L + self.R) // 2
        
        if self.L == self.R:
            self.total = val
            return
        elif index <= M:
            self.left.update(index, val)
        else:
            self.right.update(index, val)
        self.total = max(self.left.total, self.right.total)
    
    def query(self, L, R):
        if L > self.R or R < self.L:
            return
        
        if self.L >= L and self.R <= R:
            return self.total
        
        M = (self.L + self.R) // 2

        if R <= M:
            return self.left.query(L, R)
        elif L >= M + 1:
            return self.right.query(L, R)
        else:
            return max(self.left.query(L, M), self.right.query(M + 1, R))


class SegmentTree:
    
    def __init__(self, nums: List[int]):
        self.root = Node(0, len(nums) - 1, nums)
    
    def update(self, index: int, val: int) -> None:
        self.root.update(index, val)

    
    def query(self, L: int, R: int) -> int:
        return self.root.query(L, R)
    
class Solution:
    def lengthOfLIS(self, nums: List[int], k: int) -> int:
        st = SegmentTree([0] * (max(nums) + 1))

        for n in nums:
            st.update(n, 1 + st.query(max(0, n - k), n - 1))
        return st.root.total
57300