<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> nums = [8,2,4,7], limit = 4
<strong>Output:</strong> 2 
<strong>Explanation:</strong> All subarrays are: 
[8] with maximum absolute diff |8-8| = 0 &lt;= 4.
[8,2] with maximum absolute diff |8-2| = 6 &gt; 4. 
[8,2,4] with maximum absolute diff |8-2| = 6 &gt; 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 &gt; 4.
[2] with maximum absolute diff |2-2| = 0 &lt;= 4.
[2,4] with maximum absolute diff |2-4| = 2 &lt;= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 &gt; 4.
[4] with maximum absolute diff |4-4| = 0 &lt;= 4.
[4,7] with maximum absolute diff |4-7| = 3 &lt;= 4.
[7] with maximum absolute diff |7-7| = 0 &lt;= 4. 
Therefore, the size of the longest subarray is 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5
<strong>Output:</strong> 4 
<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 &lt;= 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0
<strong>Output:</strong> 3
</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] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= limit &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    class segmentTree{
        vector<int> tree;
        public:
        segmentTree(vector<int> nums){
            int n = nums.size();
            tree = vector<int>(4*n+1);
            build(nums);
        }

        void build(vector<int> &nums){
            int n = nums.size();
            for(int i=0;i<n;i++){
                update(i,0,nums.size()-1,1,nums[i]);
            }
        }

        void update(int i,int tl,int tr, int tv,int val){
            if(tl==i && tr==i){
                tree[tv] = val;
                return;
            }
            if(i>tr || i<tl){
                return;
            }
            int tm = tl +(tr-tl)/2;
            update(i,tl,tm,2*tv,val);
            update(i,tm+1,tr,2*tv+1,val);
            tree[tv] = max(tree[2*tv],tree[2*tv+1]);
        }

        int getMax(int tv,int tl,int tr,int l,int r){
            if(l<=tl && r>=tr){
                return tree[tv];
            }
            else if(l>tr || r<tl){
                return 0;
            }
            int tm = tl + (tr-tl)/2;
            int left = getMax(2*tv,tl,tm,l,r);
            int right = getMax(2*tv+1,tm+1,tr,l,r);
            return max(left,right);
        }

    };

    class segmentTreeMin{
        vector<int> tree;
        public:
        segmentTreeMin(vector<int> nums){
            int n = nums.size();
            tree = vector<int>(4*n+1);
            build(nums);
        }

        void build(vector<int> &nums){
            int n = nums.size();
            for(int i=0;i<n;i++){
                update(i,0,nums.size()-1,1,nums[i]);
            }
        }

        void update(int i,int tl,int tr, int tv,int val){
            if(tl==i && tr==i){
                tree[tv] = val;
                return;
            }
            if(i>tr || i<tl){
                return;
            }
            int tm = tl +(tr-tl)/2;
            update(i,tl,tm,2*tv,val);
            update(i,tm+1,tr,2*tv+1,val);
            tree[tv] = min(tree[2*tv],tree[2*tv+1]);
        }

        int getMax(int tv,int tl,int tr,int l,int r){
            if(l<=tl && r>=tr){
                return tree[tv];
            }
            else if(l>tr || r<tl){
                return INT_MAX;
            }
            int tm = tl + (tr-tl)/2;
            int left = getMax(2*tv,tl,tm,l,r);
            int right = getMax(2*tv+1,tm+1,tr,l,r);
            return min(left,right);
        }

    };

    int longestSubarray(vector<int>& nums, int limit) {
        segmentTree s(nums);
        segmentTreeMin sMin(nums);
        int l = 0;
        int r = 0;
        int n = nums.size();
        int ans = 1;
        while(l<=r && r<nums.size()){
            int maxVal = s.getMax(1,0,n-1,l,r);
            int minVal = sMin.getMax(1,0,n-1,l,r);
            // cout<<l<<" "<<r<<" "<<minVal<<" "<<maxVal<<endl;
            while(l<r && abs(maxVal-minVal)>limit){
                l++;
                maxVal = s.getMax(1,0,n-1,l,r);
                minVal = sMin.getMax(1,0,n-1,l,r);
            }
            ans = max(ans,r-l+1);
            r++;
        }
        return ans;
    }
};
95106