Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     #define LSB(x) (x&(-x))
  4.     #define MAX 100000
  5.    
  6.     int AIB1[MAX], AIB2[MAX];
  7.    
  8.     void update1(int pos, int val) {
  9.         while (pos < MAX) {
  10.             AIB1[pos] += val;
  11.             pos += LSB(pos);
  12.         }
  13.     }
  14.    
  15.     void update2(int pos, int val) {
  16.         while (pos < MAX) {
  17.             AIB2[pos] += val;
  18.             pos += LSB(pos);
  19.         }
  20.     }
  21.    
  22.     void add(int l, int r, int val) {
  23.         update1(l, val);
  24.         update1(r+1, -val);
  25.        
  26.         update2(l, val*(l-1));
  27.         update2(r+1, -val*r);
  28.     }
  29.    
  30.     int queryAux1(int pos) {
  31.         int sum = 0;
  32.         if (pos <= 0) return 0;
  33.         while (pos) {
  34.             sum += AIB1[pos];
  35.             pos -= LSB(pos);
  36.         }
  37.         return sum;
  38.     }
  39.    
  40.     int queryAux2(int pos) {
  41.         int sum = 0;
  42.         if (pos <= 0) return 0;
  43.         while (pos) {
  44.             sum += AIB2[pos];
  45.             pos -= LSB(pos);
  46.         }
  47.         return sum;
  48.     }
  49.    
  50.     int queryAux(int x) {
  51.         return queryAux1(x)*x - queryAux2(x);
  52.     }
  53.    
  54.     int query(int l, int r) {
  55.         return queryAux(r) - queryAux(l-1);
  56.     }
  57.    
  58.     bool isRectangleCover(vector<vector<int>>& rectangles) {
  59.         if (rectangles.size() == 0) {
  60.             return false;
  61.         }
  62.        
  63.         memset(AIB1, 0, sizeof(AIB1));
  64.         memset(AIB2, 0, sizeof(AIB2));
  65.        
  66.         int mm = 1;
  67.         for (int i = 0; i < rectangles.size(); i++) {
  68.             mm = min(mm, rectangles[i][1]);
  69.         }
  70.         if (mm < 0) {
  71.             mm = -mm; mm++;
  72.         } else {
  73.             mm = 1;
  74.         }
  75.        
  76.         for (int i = 0; i < rectangles.size(); i++) {
  77.             rectangles[i][1] += mm;
  78.             rectangles[i][3] += mm;
  79.         }
  80.        
  81.         map<int, vector<pair<pair<int, int>, int>>> H;
  82.         for (int i = 0; i < rectangles.size(); i++) {
  83.             H[rectangles[i][0]].push_back({{rectangles[i][1], rectangles[i][3]}, +1});
  84.             H[rectangles[i][2]].push_back({{rectangles[i][1], rectangles[i][3]}, -1});
  85.         }
  86.        
  87.         unordered_set<int> Hstart, Hend;
  88.        
  89.         for (auto it = H.begin(); it != H.end(); it++) {
  90.             for (auto it2 = it->second.begin(); it2 != it->second.end(); it2++) {
  91.                 if (it2->second == -1) {
  92.                     add(it2->first.first, it2->first.second, -1);
  93.                     Hstart.erase(Hstart.find(it2->first.first));
  94.                     Hend.erase(Hend.find(it2->first.second));
  95.                 }
  96.             }
  97.            
  98.             for (auto it2 = it->second.begin(); it2 != it->second.end(); it2++) {
  99.                 if (it2->second == +1) {
  100.                     int sum = query(it2->first.first, it2->first.second);
  101.                     if (sum > 2) {
  102.                         return false;
  103.                     } else if (sum == 2) {
  104.                         if (Hstart.find(it2->first.second) == Hstart.end() || Hend.find(it2->first.first) == Hend.end()) {
  105.                             return false;
  106.                         }
  107.                     } else if (sum == 1) {
  108.                         if (Hstart.find(it2->first.second) == Hstart.end() && Hend.find(it2->first.first) == Hend.end()) {
  109.                             return false;
  110.                         }
  111.                     }
  112.                     add(it2->first.first, it2->first.second, +1);
  113.                     Hstart.insert(it2->first.first);
  114.                     Hend.insert(it2->first.second);
  115.                 }
  116.             }
  117.         }
  118.        
  119.         int lmin = rectangles[0][0];
  120.         int lmax = rectangles[0][2];
  121.         int cmin = rectangles[0][1];
  122.         int cmax = rectangles[0][3];
  123.         int area = 0;
  124.         for (int i = 0; i < rectangles.size(); i++) {
  125.             lmin = min(lmin, rectangles[i][0]);
  126.             lmax = max(lmax, rectangles[i][2]);
  127.             cmin = min(cmin, rectangles[i][1]);
  128.             cmax = max(cmax, rectangles[i][3]);
  129.            
  130.             area += (rectangles[i][2] - rectangles[i][0]) * (rectangles[i][3] - rectangles[i][1]);
  131.         }
  132.        
  133.         if (area != (lmax - lmin) * (cmax - cmin)) {
  134.             return false;
  135.         }
  136.        
  137.         return true;
  138.     }
  139. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement