classRangeFreqQuery:def__init__(self,arr:List[int]):self.g=defaultdict(list)fori,xinenumerate(arr):self.g[x].append(i)defquery(self,left:int,right:int,value:int)->int:idx=self.g[value]l=bisect_left(idx,left)r=bisect_left(idx,right+1)returnr-l# Your RangeFreqQuery object will be instantiated and called as such:# obj = RangeFreqQuery(arr)# param_1 = obj.query(left,right,value)
classRangeFreqQuery{privateMap<Integer,List<Integer>>g=newHashMap<>();publicRangeFreqQuery(int[]arr){for(inti=0;i<arr.length;++i){g.computeIfAbsent(arr[i],k->newArrayList<>()).add(i);}}publicintquery(intleft,intright,intvalue){if(!g.containsKey(value)){return0;}varidx=g.get(value);intl=Collections.binarySearch(idx,left);l=l<0?-l-1:l;intr=Collections.binarySearch(idx,right+1);r=r<0?-r-1:r;returnr-l;}}/** * Your RangeFreqQuery object will be instantiated and called as such: * RangeFreqQuery obj = new RangeFreqQuery(arr); * int param_1 = obj.query(left,right,value); */
classRangeFreqQuery{public:RangeFreqQuery(vector<int>&arr){for(inti=0;i<arr.size();++i){g[arr[i]].push_back(i);}}intquery(intleft,intright,intvalue){if(!g.contains(value)){return0;}auto&idx=g[value];autol=lower_bound(idx.begin(),idx.end(),left);autor=lower_bound(idx.begin(),idx.end(),right+1);returnr-l;}private:unordered_map<int,vector<int>>g;};/** * Your RangeFreqQuery object will be instantiated and called as such: * RangeFreqQuery* obj = new RangeFreqQuery(arr); * int param_1 = obj->query(left,right,value); */
typeRangeFreqQuerystruct{gmap[int][]int}funcConstructor(arr[]int)RangeFreqQuery{g:=make(map[int][]int)fori,v:=rangearr{g[v]=append(g[v],i)}returnRangeFreqQuery{g}}func(this*RangeFreqQuery)Query(leftint,rightint,valueint)int{ifidx,ok:=this.g[value];ok{l:=sort.SearchInts(idx,left)r:=sort.SearchInts(idx,right+1)returnr-l}return0}/** * Your RangeFreqQuery object will be instantiated and called as such: * obj := Constructor(arr); * param_1 := obj.Query(left,right,value); */
classRangeFreqQuery{privateg:Map<number,number[]>=newMap();constructor(arr:number[]){for(leti=0;i<arr.length;++i){if(!this.g.has(arr[i])){this.g.set(arr[i],[]);}this.g.get(arr[i])!.push(i);}}query(left:number,right:number,value:number):number{constidx=this.g.get(value);if(!idx){return0;}constsearch=(x:number):number=>{let[l,r]=[0,idx.length];while(l<r){constmid=(l+r)>>1;if(idx[mid]>=x){r=mid;}else{l=mid+1;}}returnl;};constl=search(left);constr=search(right+1);returnr-l;}}/** * Your RangeFreqQuery object will be instantiated and called as such: * var obj = new RangeFreqQuery(arr) * var param_1 = obj.query(left,right,value) */