<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 {
    struct value_info {
        long long value{};
        int idx{};
    };

    struct value_info_less {
        bool operator()(value_info l, value_info r) {
            return l.value < r.value;
        }
    };

    struct value_info_greater {
        bool operator()(value_info l, value_info r) {
            return l.value > r.value;
        }
    };

    template <typename Comp> class heap {
    public:
        void push(value_info val) {
            container.push_back(val);
            index_tracker[val.idx] = container.size() - 1;
            heapify_up(container.size() - 1);
        }

        bool empty() const { return container.empty(); }

        value_info top() const { return container[0]; }

        int size() const { return container.size(); }

        bool exist(int original_index) {
            return index_tracker[original_index] != -1;
        }

        void pop() {
            swap(container.front(), container.back());
            index_tracker[container.front().idx] = 0;
            index_tracker[container.back().idx] = -1;
            container.pop_back();
            heapify_down(0);
        }

        void remove(int original_index) {
            int i = index_tracker[original_index];
            assert(i < container.size());
            to_root(i);
            pop();
        }

    private:
        void to_root(int i) {
            if (i == 0) {
                return;
            }

            int p = parent(i);
            if (p >= 0) {
                index_tracker[container[p].idx] = i;
                index_tracker[container[i].idx] = p;
                swap(container[p], container[i]);
                to_root(p);
            }
        }
        void heapify_up(int i) {
            int p = parent(i);
            if (p >= 0) {
                Comp cmp;
                if (cmp(container[p], container[i])) {
                    index_tracker[container[p].idx] = i;
                    index_tracker[container[i].idx] = p;
                    swap(container[p], container[i]);
                    heapify_up(p);
                }
            }
        }
        void heapify_down(int i) {
            if (container.empty()) {
                return;
            }

            int l = left(i);
            int r = right(i);

            int next = -1;
            Comp cmp;
            if (l < container.size() && cmp(container[i], container[l])) {
                next = l;
            } else {
                next = i;
            }

            if (r < container.size() && cmp(container[next], container[r])) {
                next = r;
            }

            if (next != i) {
                index_tracker[container[next].idx] = i;
                index_tracker[container[i].idx] = next;
                swap(container[next], container[i]);
                heapify_down(next);
            }
        }

        int parent(int i) { return (i - 1) / 2; }
        int left(int i) { return 2 * i + 1; }
        int right(int i) { return 2 * i + 2; }

    private:
        vector<value_info> container;
        unordered_map<int, int> index_tracker;
    };

    class MinMaxHeap {
        using MaxHeap = heap<value_info_less>;
        using MinHeap = heap<value_info_greater>;

        MaxHeap mMaxHeap;
        MinHeap mMinHeap;

    public:
        double median() const {
            assert(!mMaxHeap.empty());
            if (mMaxHeap.size() > mMinHeap.size()) {
                return mMaxHeap.top().value;
            }

            return (mMaxHeap.top().value + mMinHeap.top().value) / 2.0;
        }

        void remove(int value, int idx) {
            assert(!mMaxHeap.empty());

            if (mMaxHeap.exist(idx)) {
                mMaxHeap.remove(idx);
            } else {
                mMinHeap.remove(idx);
            }

            rebalance();
        }

        void add(int value, int idx) {
            mMaxHeap.push({value, idx});
            mMinHeap.push(mMaxHeap.top());
            mMaxHeap.pop();

            rebalance();
        }

    private:
        void rebalance() {
            while (mMaxHeap.size() < mMinHeap.size()) {
                mMaxHeap.push(mMinHeap.top());
                mMinHeap.pop();
            }
        }
    };

public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        MinMaxHeap heap;

        for (int i = 0; i < k; ++i) {
            heap.add(nums[i], i);
        }

        vector<double> result;
        result.push_back(heap.median());
        for (int i = k; i < nums.size(); ++i) {
            heap.remove(nums[i - k], i - k);
            heap.add(nums[i], i);
            result.push_back(heap.median());
        }

        return result;
    }
};
54210