Python | Window + SortedList | O(nlogn)
addElement: O(log n)\ncalculateMKAverage: O(1)\n```\nfrom sortedcontainers import SortedList\nfrom collections import deque\n\nclass MKAverage:\n\n    def init(
addElement: O(log n)\ncalculateMKAverage: O(1)\n```\nfrom sortedcontainers import SortedList\nfrom collections import deque\n\nclass MKAverage:\n\n    def __init__(self, m: int, k: int):\n        self.m = m\n        self.k = k\n        self.sl = SortedList()\n        self.window = deque()\n        self.sum = 0\n\n    def addElement(self, num: int) -> None:\n        if len(self.sl) < self.m:\n            self.window.append(num)\n            self.sl.add(num)\n            if len(self.sl) == self.m:\n                self.sum = sum(self.sl[self.k:-self.k])\n        else:\n            v = self.window.popleft()\n            self.window.append(num)\n            i = self.sl.bisect_left(v)\n            j = self.sl.bisect_left(num)\n            if 0<=i<self.k:\n                if self.k < j:\n                    self.sum -= self.sl[self.k]\n                    if j <= self.m - self.k:\n                        self.sum += num\n                    else:\n                        self.sum += self.sl[self.m-self.k]\n            elif self.k <= i < self.m - self.k:\n                self.sum -= v\n                if 0<= j < self.k:\n                    self.sum += self.sl[self.k-1]\n                elif self.k <= j <= self.m-self.k:\n                    self.sum += num\n                else:\n                    self.sum += self.sl[self.m-self.k]\n            else:\n                if j < self.m - self.k:\n                    self.sum -= self.sl[self.m-self.k-1]\n                    if self.k <= j:\n                        self.sum += num\n                    else:\n                        self.sum += self.sl[self.k-1]\n            self.sl.remove(v)\n            self.sl.add(num)\n\n    def calculateMKAverage(self) -> int:\n        if len(self.sl) < self.m:\n            return -1\n        return self.sum//(self.m-2*self.k)\n        \n\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage(m, k)\n# obj.addElement(num)\n# param_2 = obj.calculateMKAverage()
['Python']