Cod sursa(job #1518300)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 5 noiembrie 2015 20:11:38
Problema Ograzi Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 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>, vector<pair<int, int> >, celula_hash> celule;
	celule.reserve(n+m);
    for(int i = 0, x, y; i < n; ++i){
        f >> x >> y;
        const int x_ = x/w, y_ = y/h;
        for(int dx = 0; dx <= 1; ++dx){
            for(int dy = 0; dy <= 1; ++dy){
                celule[make_pair(x_+dx, y_+dy)].push_back(make_pair(x, y)); } } }

    int rez = 0;
    for(int i = 0, x, y; i < m; ++i){
        f >> x >> y;
		const auto& de_ver = celule[make_pair(x/w, y/h)];
		for(const auto ograda : de_ver){
			if(included(w, h, ograda.first, ograda.second, x, y)){
				++rez; } } }
    g << rez;
    return 0; }