C++ and multisets
\nclass MKAverage {\n    queue<int> q;\n    multiset<int> minheap;\n    multiset<int> midheap;\n    multiset<int> maxheap;\n    int m;\n    int k;\n    int coun
```\nclass MKAverage {\n    queue<int> q;\n    multiset<int> minheap;\n    multiset<int> midheap;\n    multiset<int> maxheap;\n    int m;\n    int k;\n    int count;\n    long sum;\npublic:\n    MKAverage(int m, int k) {\n        this->m = m;\n        this->k = k;\n        count = 0;\n        sum = 0;\n    }\n    \n    void addElement(int num) {\n        count++;\n        q.push(num);\n        int x = q.front();\n        if (count > m) {\n            q.pop();\n            if (maxheap.count(x)) {\n                maxheap.erase(maxheap.find(x));\n                int v = *midheap.begin();\n                maxheap.insert(v);\n                sum -= v;\n                midheap.erase(midheap.find(v));\n            } else if (minheap.count(x)) {\n                minheap.erase(minheap.find(x));\n                int v = *midheap.rbegin();\n                sum -= v;\n                minheap.insert(v);\n                midheap.erase(midheap.find(v));\n            } else {\n                sum -= x;\n                midheap.erase(midheap.find(x));\n            }\n        }\n        maxheap.insert(num);\n        if (maxheap.size() > k) {\n            int v = *maxheap.rbegin();\n            maxheap.erase(maxheap.find(v));\n            minheap.insert(v);\n            if (minheap.size() > k) {\n                v = *minheap.begin();\n                minheap.erase(minheap.begin());\n                midheap.insert(v);\n                sum += v;\n            }\n        }\n    }\n    \n    int calculateMKAverage() {\n        if (count < m)\n            return -1;\n        return sum / midheap.size();\n    }\n};\n```
[]