Advertisement
erek1e

Plundering Pirates - BIO 2025 Round 2

Apr 24th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void inputOutput() {
  8.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  9.     #ifndef EREKLE
  10.         freopen("input.txt","r",stdin);
  11.         freopen("output.txt","w",stdout);
  12.     #endif
  13. }
  14.  
  15. long long INF = 1e10;
  16.  
  17. struct FenwickTree {
  18.     int n;
  19.     vector<int> fenwick;
  20.     FenwickTree(int len): n(len) {
  21.         fenwick.resize(n);
  22.     }
  23.     void update(int index, int value) {
  24.         while (index < n) {
  25.             fenwick[index] += value;
  26.             index += index & -index;
  27.         }
  28.     }
  29.     int read(int index) {
  30.         int sum = 0;
  31.         while (index > 0) {
  32.             sum += fenwick[index];
  33.             index -= index & -index;
  34.         }
  35.         return sum;
  36.     }
  37.     int range(int left, int right) { // (l, r]
  38.         return read(right) - read(left);
  39.     }
  40. };
  41.  
  42. struct Subquery {
  43.     int right; // left will be 0
  44.     int bottom, top;
  45.     int queryIndex;
  46.     int sign; // -1 or +1
  47.     Subquery(int r, int b, int t, int i, int s): right(r), bottom(b), top(t), queryIndex(i), sign(s) {}
  48. };
  49.  
  50. int main() {
  51.     inputOutput();
  52.     int p, r; cin >> p >> r;
  53.     vector<long long> x(p), y(p);
  54.     vector<pair<pair<int, int>, pair<int, int>>> rectangles(r);
  55.     int maxY;
  56.  
  57.     // input co-ordinates and compress y
  58.     {
  59.         for (int i = 0; i < p; ++i) cin >> x[i] >> y[i];
  60.  
  61.         vector<long long> u = y;
  62.         u.push_back(-1);
  63.         u.push_back(INF);
  64.         sort(u.begin(), u.end());
  65.         u.resize(unique(u.begin(), u.end()) - u.begin());
  66.         for (long long &i : y) {
  67.             i = lower_bound(u.begin(), u.end(), i) - u.begin();
  68.         }
  69.         for (int i = 0; i < r; ++i) {
  70.             auto & [x1, y1] = rectangles[i].first;
  71.             auto & [x2, y2] = rectangles[i].second;
  72.             cin >> x1 >> y1 >> x2 >> y2;
  73.             if (x1 > x2) swap(x1, x2);
  74.             if (y1 > y2) swap(y1, y2);
  75.            
  76.             y1 = lower_bound(u.begin(), u.end(), y1) - u.begin();
  77.             y2 = upper_bound(u.begin(), u.end(), y2) - u.begin() - 1;
  78.         }
  79.         maxY = u.size();
  80.     }
  81.  
  82.     vector<Subquery> subqueries;
  83.     for (int i = 0; i < r; ++i) {
  84.         // express the sum inside this rectangle as the difference between sums inside two rectangles
  85.         auto [x1, y1] = rectangles[i].first;
  86.         auto [x2, y2] = rectangles[i].second;
  87.         subqueries.emplace_back(x2, y1, y2, i, +1);
  88.         subqueries.emplace_back(x1-1, y1, y2, i, -1);
  89.     }
  90.     // sort subqueries by right (x)
  91.     sort(subqueries.begin(), subqueries.end(), [](const Subquery & q1, const Subquery & q2) {
  92.         return q1.right < q2.right;
  93.     });
  94.    
  95.     vector<pair<int, int>> points(p);
  96.     for (int i = 0; i < p; ++i) points[i] = {x[i], y[i]};
  97.     // sort points by x
  98.     sort(points.begin(), points.end());
  99.    
  100.     // iterate subqueries and points left-to-right with two pointers
  101.     FenwickTree fenwickTree(maxY);
  102.     vector<int> queryAnswers(r);
  103.     size_t nextPoint = 0, nextQuery = 0;
  104.     auto answerQueries = [&]() {
  105.         while (nextQuery < subqueries.size() && (nextPoint >= points.size() || subqueries[nextQuery].right < points[nextPoint].first)) {
  106.             if (nextQuery >= subqueries.size()) exit(0);
  107.             int d = fenwickTree.range(subqueries[nextQuery].bottom-1, subqueries[nextQuery].top);
  108.             queryAnswers[subqueries[nextQuery].queryIndex] += subqueries[nextQuery].sign * d;
  109.             nextQuery++;  
  110.         }
  111.     };
  112.    
  113.     while (nextPoint < points.size()) {
  114.         answerQueries();
  115.         // add next point to data structure
  116.         fenwickTree.update(points[nextPoint].second, +1);
  117.         ++nextPoint;  
  118.     }
  119.     answerQueries();
  120.    
  121.     for (int v : queryAnswers) cout << v << '\n';
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement