Cod sursa(job #1733714)

Utilizator tudi98Cozma Tudor tudi98 Data 25 iulie 2016 14:47:35
Problema Ograzi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.3 kb
#include <fstream>
#include <utility>
#include <map>
using namespace std;

class InputReader
{
public:
    InputReader() {}
    InputReader(const char* file_name)
    {
        input_file = fopen(file_name,"r");
        cursor = 0;
        fread(buf,SIZE,1,input_file);
    }

    inline InputReader &operator >> (int& n)
    {
        n = 0;
        while (buf[cursor] < '0' || buf[cursor] > '9')
            advance();
        while (buf[cursor] >= '0' && buf[cursor] <= '9')
        {
            n = n * 10 + buf[cursor] - '0';
            advance();
        }
        return *this;
    }

private:
    static const int SIZE = 1<<17;
    char buf[SIZE];
    int cursor = 0;
    FILE* input_file;

    void advance()
    {
        cursor++;
        if (cursor == SIZE)
        {
            cursor = 0;
            fread(buf,SIZE,1,input_file);
        }
    }
};

int N,M,W,H;
map< long long, long long > HH;
const long long h = 10000000;

bool check(pair<int,int> O,pair<int,int> P)
{
    if (O.first < P.first || O.second < P.second || O.first > P.first + W || O.second > P.second + H) return 0;
    return 1;
}

int main()
{
    InputReader fin = InputReader("ograzi.in");
    //ifstream fin("ograzi.in");
    ofstream fout("ograzi.out");

    fin >> N >> M >> W >> H;
    for (int i = 1; i <= N; i++)
    {
        int x,y,X,Y;
        fin >> x >> y;
        X = x/W*W; Y = y/H*H;
        HH[X*h+Y] = x*h+y;
    }

    int Sol = 0;
    for (int i = 1; i <= M; i++)
    {
        int x,y,X,Y;
        pair<int,int> O,D;
        fin >> x >> y;
        X = x / W * W;
        Y = y / H * H;
        O = make_pair(x,y);
        long long nr = X*h+Y;
        if (HH.find(nr) != HH.end())
            if (check(O,make_pair(HH[nr]/h,HH[nr]%h)))
                Sol++;
        X-=W;
        nr = X*h+Y;
        if (HH.find(nr) != HH.end())
            if (check(O,make_pair(HH[nr]/h,HH[nr]%h)))
                Sol++;
        Y-=H;
        nr = X*h+Y;
        if (HH.find(nr) != HH.end())
            if (check(O,make_pair(HH[nr]/h,HH[nr]%h)))
                Sol++;
        X+=W;
        nr = X*h+Y;
        if (HH.find(nr) != HH.end())
            if (check(O,make_pair(HH[nr]/h,HH[nr]%h)))
                Sol++;
    }

    fout << Sol << "\n";
}