Solution
C++ []\nclass Solution {\npublic:\n    vector<vector<int>> r;\n    vector<int> acc;\n    Solution(vector<vector<int>>& rects){\n\n        std::ios_base::sync_wi
```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> r;\n    vector<int> acc;\n    Solution(vector<vector<int>>& rects){\n\n        std::ios_base::sync_with_stdio(false);\n        cin.tie(0);\n\n        r = rects;\n        int len = rects.size();\n        acc = vector<int>(len, 0);\n\n        for(int i = 0; i < len; i++){\n            const vector<int> &v = rects[i];\n            acc[i] = ( i==0 ? 0: acc[i-1]) + (v[2] - v[0] + 1)*(v[3]-v[1]+1);\n        }\n        srand((unsigned)time(NULL));\n    }\n    vector<int> pick() {\n        int target = rand()%acc.back();\n\n        int start = 0;\n        int end = acc.size();\n        while (start < end){\n            int mid = (start+end)/2;\n            int mval = acc[mid];\n            if (target == mval){\n                start = mid + 1;\n                break;\n            }\n            else if (target > mval){\n                start = mid + 1;\n            }\n            else{\n                end = mid;\n            }\n        }\n        const vector<int> &v = r[start];\n        int dx = v[2] - v[0] + 1;\n        int x = v[0] + rand()%dx;\n        int dy = v[3] - v[1] + 1;\n        int y = v[1] + rand()%dy;\n        return {x,y};\n    }\n};\n```\n\n```Python3 []\nfrom typing import Generator\n\nclass Solution:\n  def __init__(self, rects: List[List[int]]):\n    self._rects = rects\n    self._pg = self._pick_generator()\n  \n  def _pick_generator(self) -> Generator[List[int], None, None]:\n    for a, b, x, y in self._rects:\n      for i in range (a, x+1):\n        for j in range (b, y+1):\n          yield [i, j]\n\n  def pick(self) -> List[int]:\n    try:\n      return next(self._pg)\n    except StopIteration:\n      self._pg = self._pick_generator()\n      return next(self._pg)\n```\n\n```Java []\nclass Solution {\n  public Solution(int[][] rects) {\n    this.rects = rects;\n    areas = new int[rects.length];\n    for (int i = 0; i < rects.length; ++i)\n      areas[i] = getArea(rects[i]) + (i > 0 ? areas[i - 1] : 0);\n  }\n  public int[] pick() {\n    final int target = rand.nextInt(areas[areas.length - 1]);\n    final int index = firstGreater(areas, target);\n    final int[] r = rects[index];\n    return new int[] {\n        rand.nextInt(r[2] - r[0] + 1) + r[0],\n        rand.nextInt(r[3] - r[1] + 1) + r[1],\n    };\n  }\n  private int[][] rects;\n  private int[] areas;\n  private Random rand = new Random();\n\n  private int getArea(int[] r) {\n    return (r[2] - r[0] + 1) * (r[3] - r[1] + 1);\n  }\n  private int firstGreater(int[] areas, int target) {\n    int l = 0;\n    int r = areas.length;\n    while (l < r) {\n      final int m = (l + r) / 2;\n      if (areas[m] > target)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n}\n```\n
['C++', 'Java', 'Python3']