Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.1 kB
2
Indexable
Never
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x...)
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct side {
    int x, y1, y2, t, id;
};

struct point {
    int x, y;
};

struct element {
    int id, t;
};

struct fenwick {
    
    int size;
    vector<int> tree;

    fenwick(int n) {
        size = n;
        tree.resize(size);
    };

    int sum(int r) {
        int result = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1) {
            result += tree[r];
        }
        return result;
    }

    int sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }

    void add(int x) {
        for (; x < size; x = x | (x + 1)) {
            tree[x]++;
        }
    }

};

void solve() {
    int n;
    cin >> n;
    vector<int> ys;
    vector<point> points(n);
    for (int i = 0; i < n; i++) {
        cin >> points[i].x >> points[i].y;
        ys.push_back(points[i].y);
    }
    int m;
    cin >> m;
    vector<side> sides(m * 2);
    for (int i = 0; i < m; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        sides[i * 2] = {x1, y1, y2, -1, i};
        sides[i * 2 + 1] = {x2, y1, y2, 1, i};
        ys.push_back(y1);
        ys.push_back(y2);
    }
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    unordered_map<int, int, custom_hash> mp;
    for (int &y : ys) {
        mp[y] = mp.size();
    }
    vector<element> scanline;
    for (int i = 0; i < n; i++) {
        scanline.emplace_back(i, 0);
    }
    for (int i = 0; i < m * 2; i++) {
        scanline.emplace_back(i, sides[i].t);
    }
    sort(scanline.begin(), scanline.end(), [&](element lhs, element rhs) {
        int x1, x2;
        if (lhs.t == 0) {
            x1 = points[lhs.id].x;
        } else {
            x1 = sides[lhs.id].x;
        }
        if (rhs.t == 0) {
            x2 = points[rhs.id].x;
        } else {
            x2 = sides[rhs.id].x;
        }
        return x1 < x2 || (x1 == x2 && lhs.t < rhs.t);
    });
    vector<int> ans(m);
    fenwick tr(mp.size());
    for (element &el : scanline) {
        if (el.t == 0) {
            tr.add(mp[points[el.id].y]);
        } else {
            ans[sides[el.id].id] += tr.sum(mp[sides[el.id].y1], mp[sides[el.id].y2]) * el.t;
        }
    }
    for (int &i : ans) {
        cout << i << '\n';
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tests = 1;
    // cin >> tests;
    while (tests--) {
        solve();
    }
    return 0;
}