Cod sursa(job #2338429)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 7 februarie 2019 14:22:46
Problema Ograzi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const ll INF = 9223372036854775807ll;
const int inf = 2147483647, M = 666013;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

int n, m, h, w, t;
vector<pi> mp[M+3];

int hsh(int x, int y)
{
    return (x*31+y)%M;
}

bool inside(int X, int Y, int x, int y)
{
    if(X < 0 || Y < 0)
        return 0;
    for (auto z : mp[hsh(X, Y)])
        if(x >= z.ff && x <= z.ff+h && y >= z.ss && y <= z.ss+w)
            return 1;
    return 0;
}

int main()
{
    InParser fin ("ograzi.in");
    ofstream fout ("ograzi.out");
    fin >> n >> m >> h >> w;
    for (int i = 1; i <= n; ++i){
        int x, y, X, Y;
        fin >> x >> y;
        X = x/h;
        Y = y/w;
        mp[hsh(X, Y)].push_back({x, y});
    }
    int ans = 0;
    for (int i = 1; i <= m; ++i){
        int x, y, X, Y;
        fin >> x >> y;
        X = x/h;
        Y = y/w;
        if (inside(X, Y, x, y))
            ++ans;
        else if (inside(X-1, Y, x, y))
            ++ans;
        else if (inside(X, Y-1, x, y))
            ++ans;
        else if (inside(X-1, Y-1, x, y))
            ++ans;
    }
    fout << ans << "\n";
    return 0;
}