Cod sursa(job #1333896)

Utilizator SmarandaMaria Pandele Smaranda Data 3 februarie 2015 18:15:41
Problema Ograzi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.38 kb
#include <cstdio>
#include <vector>
#include <unordered_map>

using namespace std;

const int N = 50003, M = 100003, SIZE = 64000;

int n, m, w, h;

class myIfstream {
    private :
        int cursor;
        FILE *input;
        char buffer [SIZE];
        void advance () {
            ++ cursor;
            if (cursor == SIZE) {
                cursor = 0;
                fread (buffer, 1, SIZE, input);
            }
        }
    public :
        myIfstream ();
        myIfstream (char *inputName) {
            input = fopen (inputName, "r");
            cursor = 0;
            fread (buffer, 1, SIZE, input);
        }
        myIfstream &operator >> (int &value) {
            value = 0;
            while (!(buffer [cursor] >= '0' && buffer [cursor] <= '9'))
                advance ();
            while (buffer [cursor] >= '0' && buffer [cursor] <= '9') {
                value = value * 10 + buffer [cursor] - '0';
                advance ();
            }
            return *this;
        }
};

struct Dreptunghi {
    int x1, y1, x2, y2;
};

class Point {
    public :
        int x, y;
        bool operator == (const Point &other)const {
            return (this -> x == other.x && this -> y == other.y);
        }
};

const int H = 666013;

struct myHash {
    size_t operator () (const Point &A) const {
        size_t value = 1;
       /* value = value * H + hash <int>()(A.x);
        value = value * H + hash <int>()(A.y);*/
        value = hash <int> ()(hash <int> ()(A.x) * H + hash <int> ()(A.y));
        return value;
    }
};

Dreptunghi D [N], R, temp;
unordered_map <Point, vector <Dreptunghi>, myHash> Hash;

int main () {
    int i, x, y, X, Y, num = 0, flag, m1, m2;
    Point B, C;
    vector <Dreptunghi> :: iterator it;

    myIfstream fin ("ograzi.in");
    freopen ("ograzi.out", "w", stdout);

    fin >> n >> m >> w >> h;
    for (i = 1; i <= n; i ++) {
        fin >> D [i].x1 >> D [i].y1;
        D [i].x2 = D [i].x1 + w;
        D [i].y2 = D [i].y1 + h;
        m1 = D [i].x1 % w;
        m2 = D [i].y1 % h;
        if (m1 == 0 && m2 == 0) {
            B.x = D [i].x1;
            B.y = D [i].y1;
            Hash [B].push_back (D [i]);

            B.x = D [i].x1;
            B.y = D [i].y2;
            Hash [B].push_back (D [i]);

            B.x = D [i].x2;
            B.y = D [i].y1;
            Hash [B].push_back (D [i]);

            B.x = D [i].x2;
            B.y = D [i].y2;
            Hash [B].push_back (D [i]);
        }

        else
            if (m1 == 0) {
                B.x = D [i].x1;
                B.y = (D [i].y1 / h + 1) * h;
                Hash [B].push_back (D [i]);

                B.x = D [i].x2;
                Hash [B].push_back (D [i]);
            }

            else
                if (m2 == 0) {
                    B.y = D [i].y1;
                    B.x = (D [i].x1 / w + 1) * w;
                    Hash [B].push_back (D [i]);

                    B.y = D [i].y2;
                    Hash [B].push_back (D [i]);
                }

                else {
                    B.x = (D [i].x1 / w + 1) * w;
                    B.y = (D [i].y1 / h + 1) * h;
                    Hash [B].push_back (D [i]);
                }

    }
    for (i = 1; i <= m; i ++) {
        fin >> x >> y;
        B.x = x / w * w;
        B.y = y / h * h;
        flag = 0;
        for (it = Hash [B].begin (); it != Hash [B].end () && !flag; ++ it) {
            R = *it;
            if (x >= R.x1 && x <= R.x2 && y >= R.y1 && y <= R.y2)
                flag = 1;
        }

        B.y = B.y + h;
        for (it = Hash [B].begin (); it != Hash [B].end () && !flag; ++ it) {
            R = *it;
            if (x >= R.x1 && x <= R.x2 && y >= R.y1 && y <= R.y2)
                flag = 1;
        }

        B.x = B.x + w;
        B.y = B.y - h;
        for (it = Hash [B].begin (); it != Hash [B].end () && !flag; ++ it) {
            R = *it;
            if (x >= R.x1 && x <= R.x2 && y >= R.y1 && y <= R.y2)
                flag = 1;
        }

        B.y = B.y + h;
        for (it = Hash [B].begin (); it != Hash [B].end () && !flag; ++ it) {
            R = *it;
            if (x >= R.x1 && x <= R.x2 && y >= R.y1 && y <= R.y2)
                flag = 1;
        }
        num = num + flag;
    }
    printf ("%d\n", num);

    return 0;
}