Cod sursa(job #2025800)

Utilizator lflorin29Florin Laiu lflorin29 Data 23 septembrie 2017 11:51:55
Problema Ograzi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.32 kb
#include <bits/stdc++.h>

using namespace std;

#define ll long long
int n, m, w, h, sol;

unordered_map<int, pair <int, int> > have;

const int P = 1e9 + 7;
 class InputReader {
    public:
        InputReader() {}
        InputReader(const char *file_name) {
            input_file = fopen(file_name, "r");
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
        inline InputReader &operator >>(int &n) {
            while(buffer[cursor] < '0' || buffer[cursor] > '9') {
                advance();
            }
            n = 0;
            while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
                n = n * 10 + buffer[cursor] - '0';
                advance();
            }
            return *this;
        }
    private:
        FILE *input_file;
        static const int SIZE = 1 << 17;
        int cursor;
        char buffer[SIZE];
        inline void advance() {
            ++ cursor;
            if(cursor == SIZE) {
                cursor = 0;
                fread(buffer, SIZE, 1, input_file);
            }
        }
};
int cod(int x, int y) {
    return x * P + y;
}
void addHash(int x, int y) {
    have[cod(x / w, y / h)] = {x, y};
}

bool inside(int a, int b, int c, int d, int x, int y) {
    return x >= a && x <= c && y >= b && y <= d;
}

bool inHash(ll codN) {
    return have.find(codN) != end(have);
}
int main() {
    InputReader cin("ograzi.in");
    ofstream cout("ograzi.out");
    cin >> n >> m >> w >> h;

    for(int i = 1; i <= n; ++i) {
        int x, y;
        cin >> x >> y;
        addHash(x, y);
    }

    for(int i = 1; i <= m; ++i) {
        int x, y;
        cin >> x >> y;
        int cx = x / w, cy = y / h;
        vector <pair<int, int> > check;
        check.emplace_back(cx, cy), check.emplace_back(cx - 1, cy);
        check.emplace_back(cx, cy - 1), check.emplace_back(cx - 1, cy - 1);

        for(auto it : check) {
            ll itcod = cod(it.first, it.second);

            if(inHash (itcod)) {
                int chkx = have[itcod].first, chky = have[itcod].second;

                if(inside(chkx, chky, chkx + w, chky + h, x, y)) {
                    ++sol;
                    break;
                }
            }
        }
    }

    cout << sol;
    return 0;
}