scala SortedSet
scala []\nclass MKAverage(_m: Int, _k: Int) {\n  import scala.util.chaining._\n  import collection.mutable\n  val (lo,mid,hi) = (mutable.TreeSet.empty[(Int,Int)
```scala []\nclass MKAverage(_m: Int, _k: Int) {\n  import scala.util.chaining._\n  import collection.mutable\n  val (lo,mid,hi) = (mutable.TreeSet.empty[(Int,Int)],mutable.TreeSet.empty[(Int,Int)],mutable.TreeSet.empty[(Int,Int)])\n  val q = mutable.Queue.empty[(Int,Int)]\n  var (sumMid,i) = (0,0)\n  def toLo():Unit = mid.head.pipe{case a@(v,_) => lo += a; sumMid -= v; mid -= a}\n  def fromLo():Unit = lo.last.pipe{case a@(v,_) => mid += a; sumMid += v; lo -= a}\n  def toHi():Unit = mid.last.pipe{case a@(v,_) => hi += a; sumMid -= v; mid -= a}\n  def fromHi():Unit = hi.head.pipe{case a@(v,_) => mid += a; sumMid += v; hi -= a}\n  def addElement(num: Int): Unit = {\n    i+=1\n    val pair = (num,i)\n    q.addOne(pair); mid += pair; sumMid += pair._1\n    if(i == _m) Iterator.range(0,_k).foreach{_ => toLo(); toHi()}\n    else if(i > _m) {\n      fromLo(); fromHi(); toLo(); toHi();\n      val h = q.dequeue()\n      if(hi.contains(h)) { hi -= h; toHi()}\n      else if(lo.contains(h)) { lo -= h; toLo()}\n      else {mid -= h; sumMid -= h._1}\n    }\n  }\n  def calculateMKAverage(): Int = if(i<_m) -1 else sumMid / mid.size\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * val obj = new MKAverage(m, k)\n * obj.addElement(num)\n * val param_2 = obj.calculateMKAverage()\n */\n```
['Design', 'Queue', 'Data Stream', 'Ordered Set', 'Scala']