fromsortedcontainersimportSortedListclassMKAverage:def__init__(self,m:int,k:int):self.m=mself.k=kself.s=0self.q=deque()self.lo=SortedList()self.mid=SortedList()self.hi=SortedList()defaddElement(self,num:int)->None:ifnotself.loornum<=self.lo[-1]:self.lo.add(num)elifnotself.hiornum>=self.hi[0]:self.hi.add(num)else:self.mid.add(num)self.s+=numself.q.append(num)iflen(self.q)>self.m:x=self.q.popleft()ifxinself.lo:self.lo.remove(x)elifxinself.hi:self.hi.remove(x)else:self.mid.remove(x)self.s-=xwhilelen(self.lo)>self.k:x=self.lo.pop()self.mid.add(x)self.s+=xwhilelen(self.hi)>self.k:x=self.hi.pop(0)self.mid.add(x)self.s+=xwhilelen(self.lo)<self.kandself.mid:x=self.mid.pop(0)self.lo.add(x)self.s-=xwhilelen(self.hi)<self.kandself.mid:x=self.mid.pop()self.hi.add(x)self.s-=xdefcalculateMKAverage(self)->int:return-1iflen(self.q)<self.melseself.s//(self.m-2*self.k)# Your MKAverage object will be instantiated and called as such:# obj = MKAverage(m, k)# obj.addElement(num)# param_2 = obj.calculateMKAverage()
classMKAverage{privateintm,k;privatelongs;privateintsize1,size3;privateDeque<Integer>q=newArrayDeque<>();privateTreeMap<Integer,Integer>lo=newTreeMap<>();privateTreeMap<Integer,Integer>mid=newTreeMap<>();privateTreeMap<Integer,Integer>hi=newTreeMap<>();publicMKAverage(intm,intk){this.m=m;this.k=k;}publicvoidaddElement(intnum){if(lo.isEmpty()||num<=lo.lastKey()){lo.merge(num,1,Integer::sum);++size1;}elseif(hi.isEmpty()||num>=hi.firstKey()){hi.merge(num,1,Integer::sum);++size3;}else{mid.merge(num,1,Integer::sum);s+=num;}q.offer(num);if(q.size()>m){intx=q.poll();if(lo.containsKey(x)){if(lo.merge(x,-1,Integer::sum)==0){lo.remove(x);}--size1;}elseif(hi.containsKey(x)){if(hi.merge(x,-1,Integer::sum)==0){hi.remove(x);}--size3;}else{if(mid.merge(x,-1,Integer::sum)==0){mid.remove(x);}s-=x;}}for(;size1>k;--size1){intx=lo.lastKey();if(lo.merge(x,-1,Integer::sum)==0){lo.remove(x);}mid.merge(x,1,Integer::sum);s+=x;}for(;size3>k;--size3){intx=hi.firstKey();if(hi.merge(x,-1,Integer::sum)==0){hi.remove(x);}mid.merge(x,1,Integer::sum);s+=x;}for(;size1<k&&!mid.isEmpty();++size1){intx=mid.firstKey();if(mid.merge(x,-1,Integer::sum)==0){mid.remove(x);}s-=x;lo.merge(x,1,Integer::sum);}for(;size3<k&&!mid.isEmpty();++size3){intx=mid.lastKey();if(mid.merge(x,-1,Integer::sum)==0){mid.remove(x);}s-=x;hi.merge(x,1,Integer::sum);}}publicintcalculateMKAverage(){returnq.size()<m?-1:(int)(s/(q.size()-k*2));}}/** * Your MKAverage object will be instantiated and called as such: * MKAverage obj = new MKAverage(m, k); * obj.addElement(num); * int param_2 = obj.calculateMKAverage(); */
classMKAverage{public:MKAverage(intm,intk){this->m=m;this->k=k;}voidaddElement(intnum){if(lo.empty()||num<=*lo.rbegin()){lo.insert(num);}elseif(hi.empty()||num>=*hi.begin()){hi.insert(num);}else{mid.insert(num);s+=num;}q.push(num);if(q.size()>m){intx=q.front();q.pop();if(lo.find(x)!=lo.end()){lo.erase(lo.find(x));}elseif(hi.find(x)!=hi.end()){hi.erase(hi.find(x));}else{mid.erase(mid.find(x));s-=x;}}while(lo.size()>k){intx=*lo.rbegin();lo.erase(prev(lo.end()));mid.insert(x);s+=x;}while(hi.size()>k){intx=*hi.begin();hi.erase(hi.begin());mid.insert(x);s+=x;}while(lo.size()<k&&mid.size()){intx=*mid.begin();mid.erase(mid.begin());s-=x;lo.insert(x);}while(hi.size()<k&&mid.size()){intx=*mid.rbegin();mid.erase(prev(mid.end()));s-=x;hi.insert(x);}}intcalculateMKAverage(){returnq.size()<m?-1:s/(q.size()-k*2);}private:intm,k;longlongs=0;queue<int>q;multiset<int>lo,mid,hi;};/** * Your MKAverage object will be instantiated and called as such: * MKAverage* obj = new MKAverage(m, k); * obj->addElement(num); * int param_2 = obj->calculateMKAverage(); */
typeMKAveragestruct{lo,mid,hi*redblacktree.Treeq[]intm,k,sintsize1,size3int}funcConstructor(mint,kint)MKAverage{lo:=redblacktree.NewWithIntComparator()mid:=redblacktree.NewWithIntComparator()hi:=redblacktree.NewWithIntComparator()returnMKAverage{lo,mid,hi,[]int{},m,k,0,0,0}}func(this*MKAverage)AddElement(numint){merge:=func(rbt*redblacktree.Tree,key,valueint){ifv,ok:=rbt.Get(key);ok{nxt:=v.(int)+valueifnxt==0{rbt.Remove(key)}else{rbt.Put(key,nxt)}}else{rbt.Put(key,value)}}ifthis.lo.Empty()||num<=this.lo.Right().Key.(int){merge(this.lo,num,1)this.size1++}elseifthis.hi.Empty()||num>=this.hi.Left().Key.(int){merge(this.hi,num,1)this.size3++}else{merge(this.mid,num,1)this.s+=num}this.q=append(this.q,num)iflen(this.q)>this.m{x:=this.q[0]this.q=this.q[1:]if_,ok:=this.lo.Get(x);ok{merge(this.lo,x,-1)this.size1--}elseif_,ok:=this.hi.Get(x);ok{merge(this.hi,x,-1)this.size3--}else{merge(this.mid,x,-1)this.s-=x}}for;this.size1>this.k;this.size1--{x:=this.lo.Right().Key.(int)merge(this.lo,x,-1)merge(this.mid,x,1)this.s+=x}for;this.size3>this.k;this.size3--{x:=this.hi.Left().Key.(int)merge(this.hi,x,-1)merge(this.mid,x,1)this.s+=x}for;this.size1<this.k&&!this.mid.Empty();this.size1++{x:=this.mid.Left().Key.(int)merge(this.mid,x,-1)this.s-=xmerge(this.lo,x,1)}for;this.size3<this.k&&!this.mid.Empty();this.size3++{x:=this.mid.Right().Key.(int)merge(this.mid,x,-1)this.s-=xmerge(this.hi,x,1)}}func(this*MKAverage)CalculateMKAverage()int{iflen(this.q)<this.m{return-1}returnthis.s/(this.m-2*this.k)}/** * Your MKAverage object will be instantiated and called as such: * obj := Constructor(m, k); * obj.AddElement(num); * param_2 := obj.CalculateMKAverage(); */
fromsortedcontainersimportSortedListclassMKAverage:def__init__(self,m:int,k:int):self.m=mself.k=kself.sl=SortedList()self.q=deque()self.s=0defaddElement(self,num:int)->None:self.q.append(num)iflen(self.q)==self.m:self.sl=SortedList(self.q)self.s=sum(self.sl[self.k:-self.k])eliflen(self.q)>self.m:i=self.sl.bisect_left(num)ifi<self.k:self.s+=self.sl[self.k-1]elifself.k<=i<=self.m-self.k:self.s+=numelse:self.s+=self.sl[self.m-self.k]self.sl.add(num)x=self.q.popleft()i=self.sl.bisect_left(x)ifi<self.k:self.s-=self.sl[self.k]elifself.k<=i<=self.m-self.k:self.s-=xelse:self.s-=self.sl[self.m-self.k]self.sl.remove(x)defcalculateMKAverage(self)->int:return-1iflen(self.sl)<self.melseself.s//(self.m-self.k*2)# Your MKAverage object will be instantiated and called as such:# obj = MKAverage(m, k)# obj.addElement(num)# param_2 = obj.calculateMKAverage()