Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     mt19937 rng;
  4.     vector <vector<int>> A;
  5.     vector <int> v;
  6.      int area(vector<int>& r) {
  7.         return (r[2] - r[0] + 1) * (r[3] - r[1] + 1);
  8.     }
  9.    
  10.     Solution(vector<vector<int>>& rects) {
  11.         auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  12.         rng = mt19937(seed);
  13.         A = rects;
  14.         int totalArea=0;
  15.         for (auto r: rects) {
  16.             totalArea+=area(r);
  17.             v.push_back(totalArea);
  18.         }
  19.     }
  20.     int gen(int a, int b) {
  21.         return uniform_int_distribution<int> (a, b)(rng);
  22.     }
  23.    
  24.     vector<int> pick() {
  25.         int rnd = gen(0, v.back()-1);
  26.         int idx = upper_bound(v.begin(), v.end(), rnd) - v.begin();
  27.         int x = gen(A[idx][0], A[idx][2]);
  28.         int y = gen(A[idx][1], A[idx][3]);
  29.         vector<int> ans = {x, y};
  30.         return ans;
  31.     }
  32. };
  33.  
  34. /**
  35.  * Your Solution object will be instantiated and called as such:
  36.  * Solution* obj = new Solution(rects);
  37.  * vector<int> param_1 = obj->pick();
  38.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement