C++: Sweet&Simple solution with explanation
It\'s similar to question#528. My Explanation is here:\n\nhttps://leetcode.com/problems/random-pick-with-weight/discuss/672072/C%2B%2B%3A-Sweet-and-simple-solut
It\'s similar to question#528. My Explanation is here:\n```\nhttps://leetcode.com/problems/random-pick-with-weight/discuss/672072/C%2B%2B%3A-Sweet-and-simple-solution\n```\nHere is my implementation. For any doubt feel free to comment. \n```\nclass Solution {\n    vector<vector<int>> rect;\n    vector<int> area;\n    int n,tarea;\npublic:\n    Solution(vector<vector<int>>& rects) {\n        n = rects.size();\n        this->rect = rects;\n        for(int i=0;i<n;i++){\n            \n            int x1 = rects[i][0], x2 = rects[i][2];\n            int y1 = rects[i][1], y2 = rects[i][3];\n            int dx = x2-x1+1, dy = y2-y1+1;\n            int a = abs(dx*dy);\n            i==0?area.push_back(a):area.push_back(a+area[i-1]);\n        }\n        tarea = area[n-1];\n    }\n    \n    vector<int> pick(){\n        int randArea = rand()%tarea;\n        int idx = upper_bound(area.begin(),area.end(),randArea) - area.begin();\n        int x1 = rect[idx][0], x2 = rect[idx][2];\n        int y1 = rect[idx][1], y2 = rect[idx][3];\n        int dx = x2-x1+1, dy = y2-y1+1;\n        int X = x1 + rand()%dx;\n        int Y = y1 + rand()%dy;\n        return {X,Y};\n        \n    }\n};\n```
[]