<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 ST:
    def __init__(self, arr):
        self.n = len(arr)
        self.nodes = 2*self.n - 1
        self.arr = arr
        self.tree = [0]*self.nodes

    def getChildren(self, v, tl, md):
        return v + 1, v + 2*(md - tl + 1)

    def ptUpdate(self, v, tl, tr, i, val):
        if tl == tr:
            self.tree[v] = max(self.tree[v], val)
            return 
        md = (tl + tr)//2
        lc, rc = self.getChildren(v, tl, md)
        if i <= md:
            self.ptUpdate(lc, tl, md, i, val)
        else:
            self.ptUpdate(rc, md + 1, tr, i, val)
        self.tree[v] = max(self.tree[lc], self.tree[rc])
    
    def rngQuery(self, v, tl, tr, l, r):
        if l > r:
            return 0
        
        elif tl == l and tr == r:
            return self.tree[v]
        
        md = (tl + tr)//2
        lc, rc = self.getChildren(v, tl, md)
        x = self.rngQuery(lc, tl, md, l, min(md, r))
        y = self.rngQuery(rc, md + 1, tr, max(l, md + 1), r)
        return max(x, y)


class Solution:
    def lengthOfLIS(self, nums: List[int], k: int) -> int:
        n = max(nums) + 1
        st = ST([0]*n)
        
        for num in nums:
            l = max(0, num - k)
            r = num - 1
            vl = st.rngQuery(0, 0, n - 1, l, r)
            st.ptUpdate(0, 0, n - 1, num, vl + 1)
        
        return st.rngQuery(0, 0, n - 1, 0, n - 1)




        
27300