<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the max sliding window</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong> 
Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       <strong>3</strong>
 1 [3  -1  -3] 5  3  6  7       <strong>3</strong>
 1  3 [-1  -3  5] 3  6  7      <strong> 5</strong>
 1  3  -1 [-3  5  3] 6  7       <strong>5</strong>
 1  3  -1  -3 [5  3  6] 7       <strong>6</strong>
 1  3  -1  -3  5 [3  6  7]      <strong>7</strong>
</pre>

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

<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [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>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution {
public:
    vector<int> st;
    vector<int> lazy;
    void propagate(int i,int l,int r){
        if(lazy[i] != 0){
            st[i] = max(lazy[i],st[i]);
            if(l != r){
                lazy[2*i+1] = max(lazy[2*i+1],lazy[i]);
                lazy[2*i+2] = max(lazy[2*i+2],lazy[i]);
            }
            lazy[i] = 0;
        }
    }
    void rangeUpdate(int i,int l,int r,int strt,int end,int val){
        propagate(i,l,r);
        if(l > r || r < strt || l > end) return ;
        if(l >= strt && r <= end){
            st[i] = max(st[i],val);
            if(l != r){
                lazy[2*i+1] = max(lazy[2*i+1],val);
                lazy[2*i+2] = max(lazy[2*i+2],val);
            }
            return ;
        }
        int mid = (r-l)/2 + l;
        rangeUpdate(2*i+1,l,mid,strt,end,val);
        rangeUpdate(2*i+2,mid + 1,r,strt,end,val);
        st[i] = max(st[2*i+1],st[2*i+2]);
    }
    int query(int i,int l,int r,int strt,int end){
        propagate(i,l,r);
        if(l > r || r < strt || l > end) return -1e9;
        if(l >= strt && r <= end){
            return st[i];
        }
        int mid = (r-l)/2 + l;
        return max(query(2*i+1,l,mid,strt,end),query(2*i+2,mid+1,r,strt,end));
        
    }
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        st.resize(4*n,-1e9);
        lazy.resize(4*n,-1e9);
        for(int i = 0;i<n;i++){
            rangeUpdate(0,0,n-1,i,i,nums[i]);
        }
        vector<int> ans;
        for(int i = 0;i+k<=n;i++){
            int j = i+k-1;
           
            ans.push_back(query(0,0,n-1,i,j));
        }
        return ans;
    }
};
170253