Cod sursa(job #769596)

Utilizator badmanDragan Dan badman Data 20 iulie 2012 09:46:01
Problema Ograzi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <stdio.h>
#include <list>

#define mod 131071
#define max 50500
using namespace std;

struct pct {
    long x, y;
};
FILE *input, *output;
char buf[16];
int n, m, w, h;
long i, j, k, pos, nr, p1, p2;
list<long> hash[mod];
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].push_back(i);
}

void read() {

    input = fopen("ograzi.in", "r");
    fscanf(input, "%d %d %d %d", &n, &m, &w, &h);
    for(i = 1; i <= n; i++) {
        fscanf(input, "%s", buf);
        dr[i].x = get_int(buf);
        fscanf(input, "%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++) {
        fscanf(input, "%s", buf);
        p.x = get_int(buf);
        fscanf(input, "%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(list<long>::iterator j = hash[pos].begin(); j != hash[pos].end(); j++) {
            q = dr[*j];
            if(q.x <= p.x && p.x <= q.x + w && q.y <= p.y && p.y <= q.y + h) {
                nr++;
                break;
            }
        }
    }
    fclose(input);
}

int main() {

    read();
    output = fopen("ograzi.out", "w");
    fprintf(output, "%ld\n", nr);
    fclose(output);
    return 0;
}