Cod sursa(job #1518585)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 5 noiembrie 2015 23:42:34
Problema Ograzi Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
#include <cstdlib>
using namespace std;
  
template <int dim>
class parsator{
    ifstream f;
    char buf[dim];
    int poz;
    void refresh(){
        if(poz == dim){
            poz = 0;
            f.read(&buf[0], dim); } }
public:
    parsator(const char* const name): f(name), poz(0){
        f.read(&buf[0], dim); }
    parsator<dim>& operator>>(int& x){
        x = 0;
        while(!isdigit(buf[poz])){
            ++poz;
            refresh(); }
        while(isdigit(buf[poz])){
            x *= 10;
            x += buf[poz] - '0';
            ++poz;
            refresh(); }
        return *this; } };
 
struct celula_hash : public unary_function<pair<int, int>, size_t>{
    size_t operator()(const pair<int, int>& n)const{
        return (hash<int>()(n.first)<<4 + 23) ^ (hash<int>()(n.second)>>1 + 13); } };

bool included(const int w, const int h, const int xdr, const int ydr, const int xp, const int yp){
    return xdr <= xp && ydr <= yp && xp <= xdr+w && yp <= ydr+h; }
  
int main(){
    parsator<1000000> f("ograzi.in");
    ofstream g("ograzi.out");
    int n, m, w, h;
    f >> n >> m >> w >> h;
	unordered_map<pair<int, int>, pair<int, int>, celula_hash> celule;
    for(int i = 0, x, y; i < n; ++i){
        f >> x >> y;
		x += w, y+=h;
        const int x_ = x/w, y_ = y/h;
		celule.emplace(make_pair(x_, y_), make_pair(x, y)); }
 
    int rez = 0;
    for(int i = 0, x, y; i < m; ++i){
        f >> x >> y;
		x += w, y+=h;

        const int x_ = x/w, y_ = y/h;
		bool is_in = false;
        for(int dx = -1; dx <= 0; ++dx){
            for(int dy = -1; dy <= 0; ++dy){
				auto ograda = celule.find(make_pair(x_+dx, y_+dy));
				if(ograda != end(celule) && included(w, h, ograda->second.first, ograda->second.second, x, y)){
					is_in = true; } } }
		if(is_in){
			++rez; } }
 
    g << rez;
    return 0; }