classSolution: def__init__(self, rects: List[List[int]]): self.rects = rects self.sum = [0] for a, b, x, y in rects: self.sum.append(self.sum[-1] + (x - a + 1) * (y - b + 1))
defpick(self) -> List[int]: k = randrange(self.sum[-1]) rectIndex = bisect_right(self.sum, k) - 1 a, b, _, y = self.rects[rectIndex] da, db = divmod(k - self.sum[rectIndex], y - b + 1) return [a + da, b + db]
classSolution { Random rand; List<Integer> arr; int[][] rects;
publicSolution(int[][] rects) { rand = newRandom(); arr = newArrayList<Integer>(); arr.add(0); this.rects = rects; for (int[] rect : rects) { inta= rect[0], b = rect[1], x = rect[2], y = rect[3]; arr.add(arr.get(arr.size() - 1) + (x - a + 1) * (y - b + 1)); } }
publicint[] pick() { intk= rand.nextInt(arr.get(arr.size() - 1)); intrectIndex= binarySearch(arr, k + 1) - 1; k -= arr.get(rectIndex); int[] rect = rects[rectIndex]; inta= rect[0], b = rect[1], y = rect[3]; intcol= y - b + 1; intda= k / col; intdb= k - col * da; returnnewint[]{a + da, b + db}; }
publicclassSolution { Random rand; IList<int> arr; int[][] rects;
publicSolution(int[][] rects) { rand = new Random(); arr = new List<int>(); arr.Add(0); this.rects = rects; foreach (int[] rect in rects) { int a = rect[0], b = rect[1], x = rect[2], y = rect[3]; arr.Add(arr[arr.Count - 1] + (x - a + 1) * (y - b + 1)); } }
publicint[] Pick() { int k = rand.Next(arr[arr.Count - 1]); int rectIndex = BinarySearch(arr, k + 1) - 1; k -= arr[rectIndex]; int[] rect = rects[rectIndex]; int a = rect[0], b = rect[1], y = rect[3]; int col = y - b + 1; int da = k / col; int db = k - col * da; returnnewint[]{a + da, b + db}; }
privateintBinarySearch(IList<int> arr, int target) { int low = 0, high = arr.Count - 1; while (low <= high) { int mid = (high - low) / 2 + low; int num = arr[mid]; if (num == target) { return mid; } elseif (num > target) { high = mid - 1; } else { low = mid + 1; } } return low; } }
classSolution { public: Solution(vector<vector<int>>& rects) : rects{rects} { this->arr.emplace_back(0); for (auto & rect : rects) { this->arr.emplace_back(arr.back() + (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1)); } } vector<int> pick(){ uniform_int_distribution<int> dis(0, arr.back() - 1); int k = dis(gen) % arr.back(); int rectIndex = upper_bound(arr.begin(), arr.end(), k) - arr.begin() - 1; k = k - arr[rectIndex]; int a = rects[rectIndex][0], b = rects[rectIndex][1]; int y = rects[rectIndex][3]; int col = y - b + 1; int da = k / col; int db = k - col * da; return {a + da, b + db}; } private: vector<int> arr; vector<vector<int>>& rects; mt19937 gen{random_device{}()}; };
int* solutionPick(Solution* obj, int* retSize) { int k = rand() % obj->arr[obj->rectsSize]; int left = 0, right = obj->rectsSize; int rectIndex = 0; while (left <= right) { int mid = (left + right) >> 1; if (obj->arr[mid] > k) { rectIndex = mid - 1; right = mid - 1; } else { left = mid + 1; } } k = k - obj->arr[rectIndex]; int a = obj->rects[rectIndex][0], b = obj->rects[rectIndex][1]; int y = obj->rects[rectIndex][3]; int col = y - b + 1; int da = k / col; int db = k - col * da; int *res = (int *)malloc(sizeof(int) * 2); res[0] = a + da; res[1] = b + db; *retSize = 2; return res; }
funcConstructor(rects [][]int) Solution { sum := make([]int, len(rects)+1) for i, r := range rects { a, b, x, y := r[0], r[1], r[2], r[3] sum[i+1] = sum[i] + (x-a+1)*(y-b+1) } return Solution{rects, sum} }
func(s *Solution) Pick() []int { k := rand.Intn(s.sum[len(s.sum)-1]) rectIndex := sort.SearchInts(s.sum, k+1) - 1 r := s.rects[rectIndex] a, b, y := r[0], r[1], r[3] da := (k - s.sum[rectIndex]) / (y - b + 1) db := (k - s.sum[rectIndex]) % (y - b + 1) return []int{a + da, b + db} }
varSolution = function(rects) { this.arr = [0]; this.rects = rects; for (const rect of rects) { const a = rect[0], b = rect[1], x = rect[2], y = rect[3]; this.arr.push(this.arr[this.arr.length - 1] + (x - a + 1) * (y - b + 1)); } };
Solution.prototype.pick = function() { let k = Math.floor(Math.random() * this.arr[this.arr.length - 1]); const rectIndex = binarySearch(this.arr, k + 1) - 1; k -= this.arr[rectIndex]; const rect = this.rects[rectIndex]; const a = rect[0], b = rect[1], y = rect[3]; const col = y - b + 1; const da = Math.floor(k / col); const db = k - col * da; return [a + da, b + db]; };