Cod sursa(job #1764861)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 25 septembrie 2016 23:27:42
Problema Ograzi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 50005;
const int kMod = 1000000007;
const int dx[4] = {0, -1, 0, -1};
const int dy[4] = {0, 0, -1, -1};

int n, m, w, h;
int xr[kMaxN];
int yr[kMaxN];
unordered_map <int, int> id;

inline int hashPair(const int x, const int y) {
    return (x * 1000 + y) % kMod;
}

inline bool insideRectangle(const int i, const int x, const int y) {
    return (xr[i] <= x && x <= xr[i] + w && yr[i] <= y && y <= yr[i] + h);
}

inline void readInt(int &v) {
    v = 0;
    register char ch = getchar();
    while (ch < '0' || '9' < ch) {
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9') {
        v = (v << 1) + (v << 3) + ch - '0';
        ch = getchar();
    }
}

int main() {
    freopen("ograzi.in", "r", stdin);
    freopen("ograzi.out", "w", stdout);
    
    readInt(n);
    readInt(m);
    readInt(w);
    readInt(h);
    for (int i = 1; i <= n; i++) {
        readInt(xr[i]);
        readInt(yr[i]);
        id[hashPair(xr[i] / w, yr[i] / h)] = i;
    }
    
    int cnt = 0;
    while (m--) {
        int x, y;
        readInt(x);
        readInt(y);
        const int gx = x / w;
        const int gy = y / h;
        bool inside = false;
        for (int i = 0; i < 4 && !inside; i++) {
            unordered_map <int, int> :: iterator it = id.find(hashPair(gx + dx[i], gy + dy[i]));
            if (it != id.end()) {
                inside |= insideRectangle(it->second, x, y);
            }
        }
        cnt += inside;
    }
    
    printf("%d\n", cnt);
    return 0;
}