2080.cpp (535B)
1 class RangeFreqQuery { 2 unordered_map<int, vector<int>> um; 3 4 public: 5 RangeFreqQuery(const vector<int> &arr) { 6 for (int i = 0; i < size(arr); i++) { 7 um[arr[i]].push_back(i); 8 }; 9 } 10 11 int query(int left, int right, int value) const { 12 const auto it = um.find(value); 13 14 static const vector<int> dummy; 15 const auto &vec = it != end(um) ? it->second : dummy; 16 17 return distance(lower_bound(begin(vec), end(vec), left), upper_bound(begin(vec), end(vec), right)); 18 } 19 };