<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p>

<ul>
	<li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li>
	<li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>

<p>You are given an integer array <code>nums</code> and an integer <code>k</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 median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</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> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
<strong>Explanation:</strong> 
Window position                Median
---------------                -----
[<strong>1  3  -1</strong>] -3  5  3  6  7        1
 1 [<strong>3  -1  -3</strong>] 5  3  6  7       -1
 1  3 [<strong>-1  -3  5</strong>] 3  6  7       -1
 1  3  -1 [<strong>-3  5  3</strong>] 6  7        3
 1  3  -1  -3 [<strong>5  3  6</strong>] 7        5
 1  3  -1  -3  5 [<strong>3  6  7</strong>]       6
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3
<strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        int n=nums.size();
        int i=0;
        priority_queue<pair<double,int>>q1,q2;
        int count1=0,count2=0;
        vector<double>ans;
        unordered_set<int>us1,us2;
        // if(k%2==0){
            int j=0;
            while(j<i+k){
                q1.push({nums[j],j});
                us1.insert(j);
                j++;
            }
            int size=q1.size();
            size/=2;
            
            while(size>0){
                q2.push({-q1.top().first,q1.top().second});
                us2.insert(q1.top().second);
                us1.erase(q1.top().second);
                q1.pop();
                size--;
            }
            while(i+k-1<n){
                while(!q1.empty()&&q1.top().second<i){
                    count1--;
                    us1.erase(q1.top().second);
                    q1.pop();
                }
               
                while(!q2.empty()&&q2.top().second<i){
                    count2--;
                     us2.erase(q2.top().second);
                    q2.pop();
                }
                double first=q1.top().first;
                
                //cout<<q1.top().second<<" "<<q2.top().second<<" ";
              if(k%2==0){double second=-q2.top().first;  ans.push_back((first+second)/2);}
              else ans.push_back(first);
                if(j>=n) break;
                // int value=nums[i];
                if(us1.find(i)!=us1.end())
                count1++;
                else if(us2.find(i)!=us2.end()) count2++;
                else cout<<"wtf";

                i++;

                while(!q1.empty()&&q1.top().second<i){
                    count1--;
                    us1.erase(q1.top().second);
                    q1.pop();
                }
               
                while(!q2.empty()&&q2.top().second<i){
                    count2--;
                     us2.erase(q2.top().second);
                    q2.pop();
                }
               
                if(q1.empty()||q1.top().first<nums[j]){
                    double value=nums[j];
                    value*=-1;
                    q2.push({value,j});
                    us2.insert(j);
                }
                else{q1.push({nums[j],j});us1.insert(j);}

                while(!q1.empty()&&!q2.empty()&&q1.top().first>(-q2.top().first)){
                     if(q2.top().second<i) {
                        q2.pop();
                        us2.erase(q2.top().second);
                        count2--;
                        continue;
                    }
                    if(q1.top().second<i) {
                        q1.pop();
                        us1.erase(q1.top().second);
                        count1--;
                        continue;
                    }
                    int value=-q2.top().first;
                    int index=q2.top().second;
                    us2.erase(index);
                    q2.push({-q1.top().first,q1.top().second});
                    
                    us2.insert(q1.top().second);
                    us1.erase(q1.top().second);
                    q1.pop();
                    q1.push({value,index});
                    us1.insert(index);
                }
                
                int size1=q1.size()-max(count1,0);
                int size2=q2.size()-max(count2,0);
                if(k%2==1) size2++;
              //  cout<<size1<<" "<<size2<<" "<<i+1<<" ";
                while(size1<size2){
                    int check=q2.top().second;
                    if(check>=i){
                    q1.push({-q2.top().first,q2.top().second});
                    us1.insert(q2.top().second);
                    us2.erase(q2.top().second);
                    q2.pop();
                    }
                    else {
                        q2.pop();
                        us2.erase(check);
                        count2--;
                    }
                    size1=q1.size()-max(count1,0);
                    size2=q2.size()-max(count2,0);
                    if(k%2==1) size2++;
                }
                while(size2<size1){
                    int check=q1.top().second;
                    if(check>=i){
                    q2.push({-q1.top().first,q1.top().second});
                    us2.insert(q1.top().second);
                    us1.erase(q1.top().second);
                    q1.pop();
                    }
                    else {
                        q1.pop();
                        us1.erase(check);
                        count1--;
                    }
                    size1=q1.size()-max(count1,0);
                    size2=q2.size()-max(count2,0);
                    if(k%2==1) size2++;
                }
                j++;
            }
       
        return ans;
    }
};
53070