Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- mt19937 rng;
- vector <vector<int>> A;
- vector <int> v;
- int area(vector<int>& r) {
- return (r[2] - r[0] + 1) * (r[3] - r[1] + 1);
- }
- Solution(vector<vector<int>>& rects) {
- auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
- rng = mt19937(seed);
- A = rects;
- int totalArea=0;
- for (auto r: rects) {
- totalArea+=area(r);
- v.push_back(totalArea);
- }
- }
- int gen(int a, int b) {
- return uniform_int_distribution<int> (a, b)(rng);
- }
- vector<int> pick() {
- int rnd = gen(0, v.back()-1);
- int idx = upper_bound(v.begin(), v.end(), rnd) - v.begin();
- int x = gen(A[idx][0], A[idx][2]);
- int y = gen(A[idx][1], A[idx][3]);
- vector<int> ans = {x, y};
- return ans;
- }
- };
- /**
- * Your Solution object will be instantiated and called as such:
- * Solution* obj = new Solution(rects);
- * vector<int> param_1 = obj->pick();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement