Cod sursa(job #1404688)

Utilizator vladrochianVlad Rochian vladrochian Data 28 martie 2015 14:17:45
Problema Ograzi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <unordered_map>
using namespace std;

typedef pair<int, int> Pair;

class Parser {
	public:
	Parser(const char *filename) {
		input.open(filename);
		input.read(buffer, kSize);
		cursor = 0;
	}
	Parser& operator>>(int &x) {
		x = 0;
		while (!isdigit(buffer[cursor]))
			advance();
		while (isdigit(buffer[cursor])) {
			x = x * 10 + buffer[cursor] - '0';
			advance();
		}
		return *this;
	}
	private:
	static const int kSize = 100000;
	char buffer[kSize];
	int cursor;
	ifstream input;
	void advance() {
		if (++cursor == kSize) {
			input.read(buffer, kSize);
			cursor = 0;
		}
	}
} fin("ograzi.in");
ofstream fout("ograzi.out");

int N, M, W, H, cnt;
struct Hash {
	size_t operator()(const Pair &p) const {
		return 10007 * p.first + p.second;
	}
};
unordered_map<Pair, Pair, Hash> mp;

bool Check(int x, int y, const Pair &p) {
	if (!mp.count(p))
		return false;
	Pair rect = mp[p];
	return rect.first <= x && x <= rect.first + W && rect.second <= y && y <= rect.second + H;
}

int main() {
	fin >> N >> M >> W >> H;
	while (N--) {
		int x, y;
		fin >> x >> y;
		mp[Pair((x - 1) / W + 1, (y - 1) / H + 1)] = Pair(x, y);
	}
	while (M--) {
		int x, y;
		fin >> x >> y;
		if (Check(x, y, Pair(x / W, y / H)) || Check(x, y, Pair(x / W + 1, y / H)) || Check(x, y, Pair(x / W, y / H + 1)) || Check(x, y, Pair(x / W + 1, y / H + 1)))
			++cnt;
	}
	fout << cnt << "\n";
	return 0;
}