Cod sursa(job #2840322)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 27 ianuarie 2022 13:30:32
Problema Ograzi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

#define xc first
#define yc second

ifstream fin("ograzi.in");
ofstream fout("ograzi.out");

const int mod = 40009, p = 107;
int n, m, lung, lat, ans, x, y, ind;
vector <pair <int, int>> loc[mod];
string s;

int myHash(int a, int b)
{
    return (a * p + b) % mod;
}
void check(int nr, int a, int b)
{
    for(auto i : loc[nr])
    {
        if(i.xc <= a && a <= i.xc + lung && i.yc <= b && b <= i.yc + lat)
        {
            ans++;
            return;
        }
    }
}

void citire(int &nr)
{
    while(s[ind] < '0' || s[ind] > '9')
        ind++;
    nr = 0;
    while(s[ind] >= '0' && s[ind] <= '9')
        nr = nr * 10 + (s[ind] - '0'), ind++;
}

int main()
{
    getline(fin, s, char(0));
    citire(n);
    citire(m);
    citire(lung);
    citire(lat);
    for(int i = 1; i <= n; i++)
    {
        citire(x);
        citire(y);
        loc[myHash(x / lung + 1, y / lat + 1)].push_back({x, y});
    }
    for(int i = 1; i <= m; i++)
    {
        citire(x);
        citire(y);
        check(myHash(x / lung + 1, y / lat + 1), x, y);
        check(myHash(x / lung, y / lat + 1), x, y);
        check(myHash(x / lung + 1, y / lat), x, y);
        check(myHash(x / lung, y / lat), x, y);
    }
    fout << ans;
    return 0;
}