<p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>

<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>

<ul>
	<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
	<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>

<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], minK, maxK &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution {
public:
    long long countSubarrays(vector<int>& nums, int minK, int maxK) {
        long long ans=0,x=-1,y=-1;
        vector<int>v1,v2,v,v3;
        for(int i=0;i<nums.size();i++){
           if(nums[i]>maxK) v.push_back(i);
           if(nums[i]<minK) v3.push_back(i);
           if(nums[i]==minK) v1.push_back(i);
           if(nums[i]==maxK) v2.push_back(i);

        }
        if(v2.empty() || v1.empty()) return 0;
    
        for(int i=0;i<nums.size();i++){

            if(nums[i]>maxK) continue;
            if(nums[i]<minK) continue;
            int k;
         //   cout<<i<<" ";
          auto it=lower_bound(v.begin(),v.end(),i)-v.begin();
          if(it==v.size()) k=nums.size();
          else k=v[it];
         
          it=lower_bound(v1.begin(),v1.end(),i)-v1.begin();
          if(it==v1.size()) break;
         
          auto itr=lower_bound(v2.begin(),v2.end(),i)-v2.begin();
          if(itr==v2.size()) break;
          auto itp=lower_bound(v3.begin(),v3.end(),i)-v3.begin();
          int kp;
          if(itp==v3.size()) kp=nums.size();
          else kp=v3[itp];
          k=min(k,kp);
           int mx=max(v2[itr],v1[it]);
           if(k<mx) continue;
           ans+=k-mx;

        }
        return ans;
    }
};
90600