Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC target("avx")
- #include "bits/stdc++.h"
- #define forn(i,n) for (int i = 0; i < (int)(n); i++)
- using namespace std;
- struct Node {
- int l, r;
- int a;
- int tl, tr;
- Node(int tl, int tr) : tl(tl), tr(tr), l(-1), r(-1), a(0) {}
- };
- vector<Node> t;
- vector<int> vers;
- void Check(int i) {
- if (t[i].tl + 1 == t[i].tr) return;
- if (t[i].l == -1) {
- int tm = (t[i].tl + t[i].tr) / 2;
- int size = t.size();
- t[i].l = size;
- t[i].r = size + 1;
- t.emplace_back(t[i].tl, tm);
- t.emplace_back(tm, t[i].tr);
- }
- }
- int Update(int i, int l, int r, int d) {
- Check(i);
- if (t[i].tl >= l && t[i].tr <= r) {
- t.push_back(t[i]);
- t.back().a += d;
- return t.size() - 1;
- } else {
- int tm = (t[i].tl + t[i].tr) / 2;
- int lAns = t[i].l;
- int rAns = t[i].r;
- if (tm > l) {
- lAns = Update(t[i].l, l, r, d);
- }
- if (r > tm) {
- rAns = Update(t[i].r, l, r, d);
- }
- t.push_back(t[i]);
- t.back().l = lAns;
- t.back().r = rAns;
- return t.size() - 1;
- }
- }
- int Get(int i, int y) {
- Check(i);
- if (t[i].tl + 1 == t[i].tr) {
- return t[i].a;
- } else {
- int tm = (t[i].tl + t[i].tr) / 2;
- if (y < tm) {
- return t[i].a + Get(t[i].l, y);
- } else {
- return t[i].a + Get(t[i].r, y);
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int n, m, a, b;
- cin >> n >> m >> a >> b;
- const int MSIZE = 300300;
- vector<vector<pair<int,int> > > yy(MSIZE);
- for (int i = 0; i < a; i++) {
- int x1, y1, x2, y2;
- cin >> x1 >> y1 >> x2 >> y2;
- yy[x1].push_back({y1, y2});
- yy[x2 + 1].push_back({-y1 - 1, -y2 - 1});
- }
- t.emplace_back(0, MSIZE);
- int cur = 0;
- for (int i = 0; i < MSIZE; i++) {
- for (auto y : yy[i]) {
- int y1 = y.first;
- int y2 = y.second;
- if (y1 >= 0) {
- cur = Update(cur, y1, y2 + 1, 1);
- } else {
- cur = Update(cur, -y1 - 1, -y2, -1);
- }
- }
- vers.push_back(cur);
- }
- while (b--) {
- int x, y;
- cin >> x >> y;
- cout << Get(vers[x], y) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement