Cod sursa(job #49007)

Utilizator andrei_infoMirestean Andrei andrei_info Data 5 aprilie 2007 11:31:28
Problema Ograzi Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.92 kb
// infoarena ograzi preoni 2007 runda 3

const fin = 'ograzi.in';
      fout = 'ograzi.out';
      nmax = 50000;
      hashmax = 100000;

type pnod  = ^tnod;
     tnod  = record
                i,j,pozdr: longint;
                next:pnod;
                end;

var dr : array[1..nmax,1..2] of longint;
    hash : array[0..hashmax] of pnod;
    n,m,w,h,rez:longint;

function aflagrila(x,h:longint):longint;
begin
aflagrila:=trunc( (x-0.5) / h) + 1;
if x - 0.5 < 0 then aflagrila:=0;
end;


procedure baga_hash(x,y,i:longint);
var p:pnod;
    poz:longint;
begin
poz:=(x+y) mod hashmax;
new(p); p^.i:=x; p^.j:=y; p^.pozdr:=i; p^.next:= hash[poz];
hash[poz]:=p;
end;

function cauta(i,j,xx,yy:longint) : boolean;
var poz, d : longint;
    p : pnod;
begin
if (i<0) or (j<0) then begin cauta:=false; exit; end;
poz:=(i+j) mod hashmax;
p:=hash[poz];
cauta:=false;
while p <> nil do
        begin
        if (p^.i = i ) and ( p^.j = j ) then
                begin
                d:=p^.pozdr;
                if (xx >= dr[d][1] ) and ( xx <= dr[d][1]+w) and ( yy >= dr[d][2]) and ( yy<= dr[d][2]+h) then
                        cauta:=true;
                end;
        p:=p^.next;
        end;
end;




procedure citire;
var i,x,y,xx,yy : longint;
begin
assign(input,fin); reset(input);
assign(output,fout); rewrite(output);
readln(n,m,w,h);
for i:=1 to n do
        begin
        readln(dr[i][1],dr[i][2]);
        x:=aflagrila(dr[i][1],w);
        y:=aflagrila(dr[i][2],h);
        baga_hash(x,y,i);
        end;
for i:=1 to m do
        begin
        readln(xx,yy);
        x:=aflagrila(xx,w);
        y:=aflagrila(yy,w);
        if cauta(x,y, xx,yy) then inc(rez);
        if cauta(x-1,y,xx,yy) then inc(rez);
        if cauta(x,y-1, xx,yy) then inc(rez);
        if cauta(x-1,y-1,xx,yy) then inc(rez);
        end;
writeln(rez);
closE(input);closE(output);
end;

begin
citire;
end.