Cod sursa(job #2827992)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 6 ianuarie 2022 18:21:53
Problema Ograzi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 kb
#include <bits/stdc++.h>

using namespace std;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

#ifdef INFOARENA
#define cin fin
#define cout fout
InParser fin("ograzi.in");
ofstream fout("ograzi.out");
#endif // INFOARENA

using pii = pair <int, int>;
class LazyBIT2D {
    int n, m;
    vector <vector <int>> pos, t;
    int lsb(int x) { return x & (-x); }
public:
    LazyBIT2D(int _n, int _m) : n(_n), m(_m), t(_n + 1), pos(_n + 1) {}
    void fake_query(int x, int y) {
        for(; x; x -= lsb(x)) if(x <= n)
            pos[x].push_back(y);
    }
    void push() {
        for(int i = 1; i <= n; i++) if(pos[i].size()) {
            sort(pos[i].begin(), pos[i].end());
            auto it = unique(pos[i].begin(), pos[i].end());
            pos[i].resize(it - pos[i].begin());
            t[i].resize(pos[i].size() + 1, 0);
        }
    }
    void add(int x, int y, int val) {
        for(; x <= n; x += lsb(x))
            for(int cy = lower_bound(pos[x].begin(), pos[x].end(), y) - pos[x].begin() + 1; cy < t[x].size(); cy += lsb(cy))
                t[x][cy] += val;
    }
    int query(int x, int y) {
        int res = 0;
        for(; x; x -= lsb(x))
            for(int cy = upper_bound(pos[x].begin(), pos[x].end(), y) - pos[x].begin(); cy; cy -= lsb(cy))
                res += t[x][cy];
        return res;
    }
};
const int N = 1e6;
vector <pii> queries;
vector <int> xx;
int lbx(int x) {
    return lower_bound(xx.begin(), xx.end(), x) - xx.begin() + 1;
}

int main()
{
    int n, m, w, h, x, y;
    cin >> n >> m >> w >> h; w++, h++;
    queries.resize(n);
    map <int, int> compx;
    for(int i = 0; i < n; i++) {
        cin >> x >> y; x += 2, y += 2;
        queries[i] = {x, y};
        compx[x - 1] = compx[x + w] = 1;
    }
    int kx = 0, ky = 2e6 + 5;
    for(auto& p : compx) p.second = ++kx, xx.push_back(p.first);
    LazyBIT2D B(kx, ky);
    for(auto [x, y] : queries) {
        B.fake_query(compx[x + w], y + h);
        B.fake_query(compx[x + w], y - 1);
        B.fake_query(compx[x - 1], y + h);
        B.fake_query(compx[x - 1], y - 1);
    }
    B.push();
    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        x += 2, y += 2;
        B.add(lbx(x), y, 1),
        B.add(lbx(x + w + 1), y, -1),
        B.add(lbx(x), y + h + 1, -1),
        B.add(lbx(x + w + 1), y + h + 1, 1);
    }
    int res = 0;
    for(auto [x, y] : queries)
        res += B.query(compx[x + w], y + h) + B.query(compx[x - 1], y - 1) -
            (B.query(compx[x + w], y - 1) + B.query(compx[x - 1], y + h));
    cout << res;
    return 0;
}