Advertisement
Guest User

C

a guest
Apr 6th, 2019
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <set>
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. typedef long long int64_t;
  14.  
  15. const int N = 100000;
  16. int n, C;
  17.  
  18. pair<int, int> points[N];
  19.  
  20. vector<int> edges[N];
  21. int component[N];
  22. int csize[N];
  23. int cc;
  24.  
  25. int main() {
  26.   cin >> n >> C;
  27.  
  28.   for (int i = 0; i < n; i++) {
  29.     int x, y;
  30.     cin >> x >> y;
  31.     points[i].first = x + y;
  32.     points[i].second = x - y;
  33.   }
  34.  
  35.   sort(points, points + n);
  36.  
  37.   set<pair<int, int>> ys;
  38.   int li = 0;
  39.   for (int i = 0; i < n; i++) {
  40.     int x = points[i].first;
  41.     int y = points[i].second;
  42.     while (li < i && points[li].first + C < x) {
  43.       ys.erase(make_pair(points[li].second, li));
  44.       li++;
  45.     }
  46.     auto it = ys.lower_bound(make_pair(y, i));
  47.     if (it != ys.begin()) {
  48.       it--;
  49.     }
  50.     for (int j = 0; j < 2 && it != ys.end(); j++, it++)
  51.     if (abs(it->first - y) <= C) {
  52.       edges[i].push_back(it->second);
  53.       edges[it->second].push_back(i);
  54.     }
  55.     ys.insert(make_pair(y, i));
  56.   }
  57.  
  58.   cc = 0;
  59.  
  60.   for (int i = 0; i < n; i++) {
  61.     if (component[i] == 0) {
  62.       component[i] = ++cc;
  63.       csize[i] = 1;
  64.       queue<int> qu;
  65.       qu.push(i);
  66.       while (!qu.empty()) {
  67.         int x = qu.front();
  68.         qu.pop();
  69.         for (auto nx : edges[x]) {
  70.           if (component[nx] == 0) {
  71.             component[nx] = cc;
  72.             csize[i]++;
  73.             qu.push(nx);
  74.           }
  75.         }
  76.       }
  77.     }
  78.   }
  79.  
  80.   int mx = 0;
  81.   for (auto x : csize) {
  82.     mx = max(x, mx);
  83.   }
  84.  
  85.   cout << cc << " " << mx << endl;
  86.  
  87.  
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement