Cod sursa(job #49121)

Utilizator andrei_infoMirestean Andrei andrei_info Data 5 aprilie 2007 13:01:44
Problema Ograzi Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.48 kb
// infoarena ograzi preoni 2007 runda 3
const fin = 'ograzi.in';
      fout = 'ograzi.out';
      nmax = 50005;
      hashmax = 666013;

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+5] of pnod;
    n,m,w,h,rez:longint;
    c:char;
    f:text;

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

end;

procedure baga_hash(x,y,i:longint);
var p:pnod;
    poz:longint;
begin
poz:=(2*x+7*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:=(2*i+7*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;
                break;
                end;
        p:=p^.next;
        end;
end;

function getnn:longint;
var rez:longint;
begin
rez:=0;
while (ord(c) >=48) and (ord(c) <= 57) do
        begin
        rez:=rez*10 + ord(c) - 48;
        read(f,c);
        end;
getnn:=rez;
while (((ord(c)  < 48) or ( ord(c) > 57))) and ( ord(c) <> 26)  do
       read(f,c);
end;

procedure citire;
var i,x,y,xx,yy : longint;
    buf:array[1..65535] of byte;
begin
assign(f,fin); reset(f); settextbuf(f,buf); read(f,c);
assign(output,fout); rewrite(output);
//readln(n,m,w,h);
n:=getnn; m:=getnn; w:=getnn; h:=getnn;

for i:=1 to n do
        begin
        //readln(dr[i][1],dr[i][2]);
        dr[i][1]:=getnn;
        dr[i][2]:=getnn;
        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);
        xx:=getnn;
        yy:=getnn;
        x:=aflagrila(xx,w);
        y:=aflagrila(yy,h);
        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.