classSolution { public: longlongcountFairPairs(vector<int>& nums, int lower, int upper){ sort(nums.begin(), nums.end()); int n = nums.size(); longlong ret = 0; for(int i = 0; i < n; i++) { int t = upper - nums[i]; int l = i + 1, r = n - 1; while(l < r) { int mid = l + r + 1 >> 1; if(nums[mid] <= t) l = mid; else r = mid - 1; } int up = l; t = lower - nums[i]; l = i + 1, r = n - 1; while(l < r) { int mid = l + r >> 1; if(nums[mid] >= t) r = mid; else l = mid + 1; } int lo = r; if(up < n && nums[i] + nums[up] <= upper && nums[i] + nums[lo] >= lower) { ret += up - lo + 1; } } return ret; } };
classSolution { public: vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& queries) { unordered_map<int, pair<int,int> > hash; int n = s.size();
for(int i = 0; i < n; i++) { int x = 0; for(int j = i; j < n && j < i + 30; j++) { // x = (x * 2) + (s[j] - '0'); x = x << 1 | (s[j] & 1); if(hash.find(x) == hash.end() || hash[x].second - hash[x].first > j - i) hash[x] = {i,j}; } }
vector<vector<int>> ret;
for(auto &q:queries) { // val ^ first = second // val ^ first ^ first = second ^ first // val = second ^ first int val = q[1] ^ q[0]; if(hash.find(val) == hash.end()) ret.push_back({-1,-1}); else ret.push_back({hash[val].first,hash[val].second}); }