|  | 
|  | 1 | +/** | 
|  | 2 | + * @param {number[]} nums | 
|  | 3 | + * @param {number} k | 
|  | 4 | + * @return {number} | 
|  | 5 | + */ | 
|  | 6 | +const lengthOfLIS = (nums, k) => { | 
|  | 7 | +  const a = nums | 
|  | 8 | +    let max = Math.max(...a), st = new SegmentTreeRMQ(max + 3), res = 0; | 
|  | 9 | +    for (const x of a) { | 
|  | 10 | +        let l = Math.max(x-k, 0), r = x; | 
|  | 11 | +        let min = st.minx(l, r), maxL = min == Number.MAX_SAFE_INTEGER ? 0 : -min; | 
|  | 12 | +        maxL++; | 
|  | 13 | +        res = Math.max(res, maxL); | 
|  | 14 | +        st.update(x, -maxL); | 
|  | 15 | +    } | 
|  | 16 | +    return res; | 
|  | 17 | +}; | 
|  | 18 | +///////////////////////////////////////////// Template /////////////////////////////////////////////////////////// | 
|  | 19 | +// using array format | 
|  | 20 | +function SegmentTreeRMQ(n) { | 
|  | 21 | +    let h = Math.ceil(Math.log2(n)), len = 2 * 2 ** h, a = Array(len).fill(Number.MAX_SAFE_INTEGER); | 
|  | 22 | +    h = 2 ** h; | 
|  | 23 | +    return { update, minx, indexOf, tree } | 
|  | 24 | +    function update(pos, v) { | 
|  | 25 | +        a[h + pos] = v; | 
|  | 26 | +        for (let i = parent(h + pos); i >= 1; i = parent(i)) propagate(i); | 
|  | 27 | +    } | 
|  | 28 | +    function propagate(i) { | 
|  | 29 | +        a[i] = Math.min(a[left(i)], a[right(i)]); | 
|  | 30 | +    } | 
|  | 31 | +    function minx(l, r) { | 
|  | 32 | +        let min = Number.MAX_SAFE_INTEGER; | 
|  | 33 | +        if (l >= r) return min; | 
|  | 34 | +        l += h; | 
|  | 35 | +        r += h; | 
|  | 36 | +        for (; l < r; l = parent(l), r = parent(r)) { | 
|  | 37 | +            if (l & 1) min = Math.min(min, a[l++]); | 
|  | 38 | +            if (r & 1) min = Math.min(min, a[--r]); | 
|  | 39 | +        } | 
|  | 40 | +        return min; | 
|  | 41 | +    } | 
|  | 42 | +    function indexOf(l, v) { | 
|  | 43 | +        if (l >= h) return -1; | 
|  | 44 | +        let cur = h + l; | 
|  | 45 | +        while (1) { | 
|  | 46 | +            if (a[cur] <= v) { | 
|  | 47 | +                if (cur >= h) return cur - h; | 
|  | 48 | +                cur = left(cur); | 
|  | 49 | +            } else { | 
|  | 50 | +                cur++; | 
|  | 51 | +                if ((cur & cur - 1) == 0) return -1; | 
|  | 52 | +                if (cur % 2 == 0) cur = parent(cur); | 
|  | 53 | +            } | 
|  | 54 | +        } | 
|  | 55 | +    } | 
|  | 56 | +    function parent(i) { | 
|  | 57 | +        return i >> 1; | 
|  | 58 | +    } | 
|  | 59 | +    function left(i) { | 
|  | 60 | +        return 2 * i; | 
|  | 61 | +    } | 
|  | 62 | +    function right(i) { | 
|  | 63 | +        return 2 * i + 1; | 
|  | 64 | +    } | 
|  | 65 | +    function tree() { | 
|  | 66 | +        return a; | 
|  | 67 | +    } | 
|  | 68 | +} | 
|  | 69 | +////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | 
|  | 70 | + | 
0 commit comments