classRangeFreqQuery:def__init__(self,arr: List[int]): self.map ={}# 数值为键,值为下标数组for i inrange(len(arr)):if arr[i]notin self.map: self.map[arr[i]]= [i]else:# 因为是从头遍历,因此加入列表是有序的 self.map[arr[i]].append(i)defquery(self,left:int,right:int,value:int) ->int:if value notin self.map:return0 indexs = self.map[value]# 利用value的下标数组是有序的这个特性,二分找left、right在数组中的位置 l =bisect_left(indexs, left) r =bisect_right(indexs, right)return r - l