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

static const int iofast = [](){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 0;
}();

struct stack_max {
  vector<int> s = {};
  vector<int> smax = {};
  void push(int x) {
    s.push_back(x);
    if (smax.size() == 0 || x >= smax.back()) {
      smax.push_back(x);
    }
  }
  int pop() {
    if (s.back() == smax.back()) {
      smax.pop_back();
    }
    int t = s.back();
    s.pop_back();
    return t;
  }
  int max() {
    return smax.back();
  }
  int size() {
    return s.size();
  }
  int back() {
    return s.back();
  }
};

struct stack_min {
  vector<int> s = {};
  vector<int> smin = {};
  void push(int x) {
    s.push_back(x);
    if (smin.size() == 0 || x <= smin.back()) {
      smin.push_back(x);
    }
  }
  int pop() {
    if (s.back() == smin.back()) {
      smin.pop_back();
    }
    int t = s.back();
    s.pop_back();
    return t;
  }
  int min() {
    return smin.back();
  }
  int size() {
    return s.size();
  }
  int back() {
    return s.back();
  }
};

struct queue_max {
  stack_max head = {};
  stack_max tail = {};
  void push(int x) {
    tail.push(x);
  }
  int pop() {
    if (head.size() == 0) {
      while (tail.size()) {
        head.push(tail.back());
        tail.pop();
      }
    }
    int t = head.back();
    head.pop();
    return t;
  }
  int query() {
    int t = {};
    if (head.size())
      t = max(t, head.max());
    if (tail.size())
      t = max(t, tail.max());
    return t;
  }
  int size() {
    return head.size() + tail.size();
  }
};

struct queue_min {
  stack_min head = {};
  stack_min tail = {};
  void push(int x) {
    tail.push(x);
  }
  int pop() {
    if (head.size() == 0) {
      while (tail.size()) {
        head.push(tail.back());
        tail.pop();
      }
    }
    int t = head.back();
    head.pop();
    return t;
  }
  int query() {
    int t = INT_MAX;
    if (head.size())
      t = min(t, head.min());
    if (tail.size())
      t = min(t, tail.min());
    return t;
  }
  int size() {
    return head.size() + tail.size();
  }
};

class Solution {
public:
  int longestSubarray(vector<int>& a, int limit) {
    queue_max qmax = {};
    queue_min qmin = {};
    int ans = {};
    for (auto x : a) {
      qmin.push(x);
      qmax.push(x);
      // cout << qmax.query() << " " << qmin.query() << endl;
      while (qmax.query()-qmin.query() > limit) {
        qmax.pop();
        qmin.pop();
      }
      ans = max(ans, qmin.size());
    }
    return ans;
  }
};

// a[r]-a[l] <= limit

91550