Cod sursa(job #942329)

Utilizator SteveStefan Eniceicu Steve Data 21 aprilie 2013 20:47:37
Problema Ograzi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>
#include <vector>
#define mod 1000003
using namespace std;

int N, M, W, H;
pair <int, int> ograzi[mod];
vector <int> map[mod];
int val1[] = {0, -1, 0, -1};
int val2[] = {0, 0, -1, -1};
char pars[17];

int Inside (int x, int y, int i)
{
    int x1 = ograzi[i].first;
    int y1 = ograzi[i].second;
    if (x >= x1 && x <= x1 + W && y >= y1 && y <= y1 + H) return 1;
    return 0;
}

int main ()
{
    ifstream fin ("ograzi.in");
    ofstream fout ("ograzi.out");
    fin >> N >> M >> W >> H;
    fin.getline (pars, 10);
    for (int i = 0; i < N; i++)
    {
        fin.getline (pars, 15);
        int x = 0, y = 0, j;
        for (j = 0; pars[j] >= '0' && pars[j] <= '9'; j++)
            x = x * 10 + pars[j] - '0';
        for (++j; pars[j] >= '0' && pars[j] <= '9'; j++)
            y = y * 10 + pars[j] - '0';
        ograzi[i].first = x;
        ograzi[i].second = y;
        x /= W, y /= H;
        map[(1LL * x * 50001 + y) % mod].push_back (i);
    }
    int cnt = 0;
    for (int i = 0; i < M; i++)
    {
        fin.getline (pars, 15);
        int x = 0, y = 0, j;
        for (j = 0; pars[j] >= '0' && pars[j] <= '9'; j++)
            x = x * 10 + pars[j] - '0';
        for (++j; pars[j] >= '0' && pars[j] <= '9'; j++)
            y = y * 10 + pars[j] - '0';
        int good = 0;
        for (int j = 0; j <= 3; j++)
            if (!good)
            {
                int alfa = x / W + val1[j], beta = y / H + val2[j];
                if (alfa >= 0 && beta >= 0)
                    for (int i = 0; i < (int) map[(1LL * alfa * 50001 + beta) % mod].size (); i++)
                        if (Inside (x, y, map[(1LL * alfa * 50001 + beta) % mod][i])) good = 1;
            }
        cnt += good;
    }
    fout << cnt;
    fout.close ();
    return 0;
}