Advertisement
dmkozyrev

acmp 97.cpp

Nov 1st, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <functional>
  5.  
  6. struct Range {
  7.     int a, b;
  8.    
  9.     Range(int a = 0, int b = 0)
  10.         : a(std::min(a, b))
  11.         , b(std::max(a, b))
  12.     { }
  13.    
  14.     bool intersect(const Range& other) {
  15.         return !(a > other.b || b < other.a);
  16.     }
  17. };
  18.  
  19. struct Rectangle {
  20.     int x_min, y_min, x_max, y_max;
  21.    
  22.     Rectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0)
  23.         : x_min(std::min(x1, x2))
  24.         , x_max(std::max(x1, x2))
  25.         , y_min(std::min(y1, y2))
  26.         , y_max(std::max(y1, y2))
  27.     { }
  28.    
  29.     Rectangle& increase(int r) {
  30.         x_min -= r;
  31.         y_min -= r;
  32.         x_max += r;
  33.         y_max += r;
  34.         return *this;
  35.     }
  36.    
  37.     static Rectangle read() {
  38.         int x1, y1, x2, y2, r;
  39.         scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &r);
  40.         return Rectangle(x1, y1, x2, y2).increase(r);
  41.     }
  42.    
  43.     Range rx() const {
  44.         return Range(x_min, x_max);
  45.     }
  46.    
  47.     Range ry() const {
  48.         return Range(y_min, y_max);
  49.     }
  50.    
  51.     bool intersect(const Rectangle& other) const {
  52.         return rx().intersect(other.rx()) && ry().intersect(other.ry());
  53.     }
  54. };
  55.  
  56. int main() {
  57.     freopen("input.txt", "rt", stdin);
  58.    
  59.     int n;
  60.     scanf("%d", &n);
  61.    
  62.     std::vector<Rectangle> rect(n);
  63.     for (auto & r : rect) {
  64.         r = Rectangle::read();
  65.     }
  66.    
  67.     int colors = 0;
  68.     std::vector<int> color(n, -1);
  69.    
  70.     std::function<void(int)> dfs = [&](int s) {
  71.         for (int i = 0; i < n; ++i) {
  72.             if (i != s && color[i] == -1 && rect[s].intersect(rect[i])) {
  73.                 color[i] = colors;
  74.                 dfs(i);
  75.             }  
  76.         }
  77.     };
  78.    
  79.     for (int i = 0; i < n; ++i) {
  80.         if (color[i] == -1) {
  81.             dfs(i);
  82.             ++colors;
  83.         }
  84.     }
  85.    
  86.     printf("%d", colors);
  87.    
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement