Advertisement
tepyotin2

Intersection Points-Cooridate Compression

Jul 4th, 2025
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX_UNIQUE_Y = 200005;
  6.  
  7. struct Event {
  8.     int x;
  9.     int y1, y2;
  10.     int type;
  11.  
  12.     Event() {}
  13.     Event(int x, int y1, int y2, int type) : x(x), y1(y1), y2(y2), type(type) {}
  14. };
  15.  
  16. int fw[MAX_UNIQUE_Y];
  17. vector<Event> events;
  18. vector<int> y_coords;
  19.  
  20. void update(int index, int value, int max_coord) {
  21.     for (; index <= max_coord; index += index & -index) {
  22.         fw[index] += value;
  23.     }
  24. }
  25.  
  26. int query(int index) {
  27.     int ans = 0;
  28.     for (; index > 0; index -= index & -index) {
  29.         ans += fw[index];
  30.     }
  31.     return ans;
  32. }
  33.  
  34. bool comp(const Event& a, const Event& b) {
  35.     if (a.x != b.x) {
  36.         return a.x < b.x;
  37.     }
  38.     return a.type < b.type;
  39. }
  40.  
  41. int get_compressed_y(int y) {
  42.     return lower_bound(y_coords.begin(), y_coords.end(), y) - y_coords.begin() + 1;
  43. }
  44.  
  45.  
  46. int main() {
  47.     ios_base::sync_with_stdio(0), cin.tie(0);
  48.  
  49.     int n;
  50.     cin >> n;
  51.  
  52.     int offset = 1000000;
  53.  
  54.     for (int i = 0; i < n; i++) {
  55.         int x1, y1, x2, y2;
  56.         cin >> x1 >> y1 >> x2 >> y2;
  57.  
  58.         x1 += offset; y1 += offset;
  59.         x2 += offset; y2 += offset;
  60.  
  61.         if (x1 == x2) {
  62.             if (y1 > y2) swap(y1, y2);
  63.             events.push_back(Event(x1, y1, y2, 1));
  64.             y_coords.push_back(y1);
  65.             y_coords.push_back(y2);
  66.         } else {
  67.             if (x1 > x2) swap(x1, x2);
  68.             events.push_back(Event(x1, y1, 0, 0));
  69.             events.push_back(Event(x2, y1, 0, 2));
  70.             y_coords.push_back(y1);
  71.         }
  72.     }
  73.  
  74.     sort(y_coords.begin(), y_coords.end());
  75.     y_coords.resize(unique(y_coords.begin(), y_coords.end()) - y_coords.begin());
  76.  
  77.     int max_compressed_y = y_coords.size();
  78.  
  79.     sort(events.begin(), events.end(), comp);
  80.  
  81.     long long ans = 0;
  82.     for (const auto& e : events) {
  83.         if (e.type == 0) {
  84.             int compressed_y = get_compressed_y(e.y1);
  85.             update(compressed_y, 1, max_compressed_y);
  86.         } else if (e.type == 2) {
  87.             int compressed_y = get_compressed_y(e.y1);
  88.             update(compressed_y, -1, max_compressed_y);
  89.         } else {
  90.             int compressed_y1 = get_compressed_y(e.y1);
  91.             int compressed_y2 = get_compressed_y(e.y2);
  92.             ans += query(compressed_y2) - query(compressed_y1 - 1);
  93.         }
  94.     }
  95.  
  96.     cout << ans << '\n';
  97.  
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement