Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #ifdef ENABLE_DEBUG
- #define DEBUG(x) std::cout << x << std::endl
- #else
- #define DEBUG(x)
- #endif
- #define fi first
- #define se second
- #define pb push_back
- #define all(x) x.begin(),x.end()
- #define rall(x) x.rbegin(),x.rend()
- using namespace std;
- using ii = pair<int, int>;
- using i64 = long long;
- const int INF = 0x3f3f3f3f;
- const i64 INFLL = 0x3f3f3f3f3f3f3f3f;
- const int N = 4e5+5;
- int tree[4*N];
- int query(int node, int l, int r, int ql, int qr) {
- if (qr < l or r < ql) return 0;
- if (ql <= l and r <= qr) return tree[node];
- int mid = (l+r)/2;
- return query(2*node, l, mid, ql, qr) + query(2*node+1, mid+1, r, ql, qr);
- }
- void update(int node, int l, int r, int idx, int x) {
- if (l == r) {
- tree[node] += x;
- return;
- }
- int mid = (l+r)/2;
- if (idx <= mid) {
- update(2 * node, l, mid, idx, x);
- } else {
- update(2 * node + 1, mid + 1, r, idx, x);
- }
- tree[node] = tree[2 * node] + tree[2 * node + 1];
- }
- int main() {
- // cin.tie(0)->sync_with_stdio(0);
- int n; cin >> n;
- vector<pair<ii, ii>> pts;
- vector<int> CX, CY;
- for (int i = 0; i < n; ++i) {
- int a, b, c, d; cin >> a >> b >> c >> d;
- CX.pb(a);
- CX.pb(c);
- CY.pb(b);
- CY.pb(d);
- pts.pb({{a, b}, {c, d}});
- }
- vector<pair<int, ii>> queries;
- int q; cin >> q;
- for (int i = 0; i < q; ++i) {
- int x, y; cin >> x >> y;
- queries.pb({i, {x, y}});
- CX.pb(x);
- CY.pb(y);
- }
- sort(all(CX));
- sort(all(CY));
- CX.erase(unique(all(CX)), CX.end());
- CY.erase(unique(all(CY)), CY.end());
- int mx = CX.size() + 1;
- int my = CY.size() + 1;
- vector<vector<int>> lst(mx + 1, vector<int>());
- vector<vector<ii>> lst2(mx + 1, vector<ii>());
- for (int i = 0; i < n; ++i) {
- auto [p, q] = pts[i];
- auto [a, b] = p;
- auto [c, d] = q;
- c = (lower_bound(all(CX), c) - CX.begin()) + 1;
- d = (lower_bound(all(CY), d) - CY.begin()) + 1;
- lst[c].pb(d);
- }
- vector<int> ans(q, -1);
- for (auto [idx, coord] : queries) {
- auto [x, y] = coord;
- x = (lower_bound(all(CX), x) - CX.begin()) + 1;
- y = (lower_bound(all(CY), y) - CY.begin()) + 1;
- lst2[x].pb({idx, y});
- }
- for (int currX = 1; currX <= mx; ++currX) {
- for (int currY : lst[currX]) {
- update(1, 1, my, currY, 1);
- }
- for (auto [idx, currY] : lst2[currX]) {
- int currAns = query(1, 1, my, 1, currY);
- ans[idx] = currAns;
- }
- }
- for (int i = 0; i < q; ++i) {
- cout << ans[i] << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment