danielvitor23

N. How many rectangles?

Jun 21st, 2024
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef ENABLE_DEBUG
  3.   #define DEBUG(x) std::cout << x << std::endl
  4. #else
  5.   #define DEBUG(x)
  6. #endif
  7. #define fi first
  8. #define se second
  9. #define pb push_back
  10. #define all(x) x.begin(),x.end()
  11. #define rall(x) x.rbegin(),x.rend()
  12. using namespace std;
  13. using ii = pair<int, int>;
  14. using i64 = long long;
  15. const int INF = 0x3f3f3f3f;
  16. const i64 INFLL = 0x3f3f3f3f3f3f3f3f;
  17.  
  18. const int N = 4e5+5;
  19.  
  20. int tree[4*N];
  21.  
  22. int query(int node, int l, int r, int ql, int qr) {
  23.   if (qr < l or r < ql) return 0;
  24.   if (ql <= l and r <= qr) return tree[node];
  25.   int mid = (l+r)/2;
  26.   return query(2*node, l, mid, ql, qr) + query(2*node+1, mid+1, r, ql, qr);
  27. }
  28.  
  29. void update(int node, int l, int r, int idx, int x) {
  30.   if (l == r) {
  31.     tree[node] += x;
  32.     return;
  33.   }
  34.   int mid = (l+r)/2;
  35.   if (idx <= mid) {
  36.     update(2 * node, l, mid, idx, x);
  37.   } else {
  38.     update(2 * node + 1, mid + 1, r, idx, x);
  39.   }
  40.   tree[node] = tree[2 * node] + tree[2 * node + 1];
  41. }
  42.  
  43. int main() {
  44.   // cin.tie(0)->sync_with_stdio(0);
  45.  
  46.   int n; cin >> n;
  47.  
  48.   vector<pair<ii, ii>> pts;
  49.  
  50.   vector<int> CX, CY;
  51.   for (int i = 0; i < n; ++i) {
  52.     int a, b, c, d; cin >> a >> b >> c >> d;
  53.     CX.pb(a);
  54.     CX.pb(c);
  55.     CY.pb(b);
  56.     CY.pb(d);
  57.  
  58.     pts.pb({{a, b}, {c, d}});
  59.   }
  60.  
  61.   vector<pair<int, ii>> queries;
  62.  
  63.   int q; cin >> q;
  64.   for (int i = 0; i < q; ++i) {
  65.     int x, y; cin >> x >> y;
  66.     queries.pb({i, {x, y}});
  67.  
  68.     CX.pb(x);
  69.     CY.pb(y);
  70.   }
  71.  
  72.   sort(all(CX));
  73.   sort(all(CY));
  74.  
  75.   CX.erase(unique(all(CX)), CX.end());
  76.   CY.erase(unique(all(CY)), CY.end());
  77.  
  78.   int mx = CX.size() + 1;
  79.   int my = CY.size() + 1;
  80.  
  81.   vector<vector<int>> lst(mx + 1, vector<int>());
  82.   vector<vector<ii>> lst2(mx + 1, vector<ii>());
  83.  
  84.   for (int i = 0; i < n; ++i) {
  85.     auto [p, q] = pts[i];
  86.     auto [a, b] = p;
  87.     auto [c, d] = q;
  88.  
  89.     c = (lower_bound(all(CX), c) - CX.begin()) + 1;
  90.     d = (lower_bound(all(CY), d) - CY.begin()) + 1;
  91.  
  92.     lst[c].pb(d);
  93.   }
  94.  
  95.   vector<int> ans(q, -1);
  96.   for (auto [idx, coord] : queries) {
  97.     auto [x, y] = coord;
  98.  
  99.     x = (lower_bound(all(CX), x) - CX.begin()) + 1;
  100.     y = (lower_bound(all(CY), y) - CY.begin()) + 1;
  101.  
  102.     lst2[x].pb({idx, y});
  103.   }
  104.  
  105.   for (int currX = 1; currX <= mx; ++currX) {
  106.     for (int currY : lst[currX]) {
  107.       update(1, 1, my, currY, 1);
  108.     }
  109.  
  110.     for (auto [idx, currY] : lst2[currX]) {
  111.       int currAns = query(1, 1, my, 1, currY);
  112.  
  113.       ans[idx] = currAns;
  114.     }
  115.   }
  116.  
  117.   for (int i = 0; i < q; ++i) {
  118.     cout << ans[i] << '\n';
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment