Cod sursa(job #769601)

Utilizator badmanDragan Dan badman Data 20 iulie 2012 10:29:58
Problema Ograzi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <stdio.h>

#define mod 131071
#define max 50010

using namespace std;

struct pct {
    long x, y;
};

long hash[mod + 40][10], line[mod + 40];
char buf[16];
int n, m, w, h;
long i, j, k, pos, nr, p1, p2;
pct dr[max], p, q;

int get_int(const char *s) {
	int rez = 0, i;

	for (i = 0; s[i]; ++i)
		rez = rez * 10 + (s[i] - '0');

	return rez;
}

void hash_fct(long x, long y)
{
    long p1, p2, sol;
    p1 = x / w;
    p2 = y / h;
    if(x % w == 0)
        p1--;
    if(y % h == 0)
        p2--;
    sol = (p1 * 103 + p2) % mod;
    hash[sol][++line[sol]] = i;
}

void read() {

    freopen("ograzi.in", "r", stdin);
    scanf("%d %d %d %d", &n, &m, &w, &h);
    for(i = 1; i <= n; i++) {
        scanf("%s", buf);
        dr[i].x = get_int(buf);
        scanf("%s", buf);
        dr[i].y = get_int(buf);
        dr[i].x++;
        dr[i].y++;
        hash_fct(dr[i].x, dr[i].y);
        hash_fct(dr[i].x + w, dr[i].y);
        hash_fct(dr[i].x, dr[i].y + h);
        hash_fct(dr[i].x + w, dr[i].y + h);
    }
    for(i = 1; i <= m; i++) {
        scanf("%s", buf);
        p.x = get_int(buf);
        scanf("%s", buf);
        p.y = get_int(buf);
        p.x++;
        p.y++;
        p1 = p.x / w;
        p2 = p.y / h;
        if(p.x % w == 0)
            p1--;
        if(p.y % h == 0)
            p2--;
        pos = (p1 * 103 + p2) % mod;

        for(j = 1; j <= line[pos]; j++) {
            q = dr[hash[pos][j]];
            if(q.x <= p.x && p.x <= q.x + w && q.y <= p.y && p.y <= q.y + h) {
                nr++;
                break;
            }
        }
    }
}

int main() {

    read();
    freopen("ograzi.out", "w", stdout);
    printf("%ld\n", nr);

    return 0;
}