Cod sursa(job #2827961)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 6 ianuarie 2022 17:30:09
Problema Ograzi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.78 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 FakeBIT2D {
    int n, m;
    vector <vector <int>> pos, t;
    int lsb(int x) { return x & (-x); }
public:
    FakeBIT2D(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;

int main()
{
    int n, m, w, h, x, y;
    cin >> n >> m >> w >> h;
    FakeBIT2D B(N + 1, N + 1); queries.resize(n);
    for(int i = 0; i < n; i++) {
        cin >> x >> y; x++, y++;
        queries[i] = {x, y};
        B.fake_query(x + w, y + h);
        B.fake_query(x + w, y);
        B.fake_query(x, y + h);
        B.fake_query(x, y);
    }
    B.push();
    for(int i = 0; i < m; i++)
        cin >> x >> y,
        B.add(x, y, 1),
        B.add(x + w, y, -1),
        B.add(x, y + h, -1),
        B.add(x + w, y + h, 1);
    int res = 0;
    for(int i = 0; i < n; i++)
        res += B.query(queries[i].first + w, queries[i].second + h) + B.query(queries[i].first, queries[i].second) -
            (B.query(queries[i].first + w, queries[i].second) + B.query(queries[i].first, queries[i].second + h));
    cout << res;
    return 0;
}