Cod sursa(job #21782)

Utilizator Programmer01Mierla Laurentiu Marian Programmer01 Data 24 februarie 2007 13:54:38
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.77 kb
program p1;
type tip=0..250001;
var mat:array[0..250001,0..17] of tip;
    n,m,i,j,p,q,x,y:0..350001;
    f,g:text;
begin
assign(f,'stramosi.in');
reset(f);
assign(g,'stramosi.out');
rewrite(g);
read(f,n,m);
for i:=1 to n do
read(f,mat[i,1]);
for i:=1 to n do
begin
j:=1;
p:=0;
repeat
j:=j+1;
x:=mat[i,j-1];
if x=0 then p:=1
else
begin
q:=mat[x,j-1];
if q=0 then p:=1
else mat[i,j]:=q;
end;
until p=1;
mat[i,0]:=1 shl (j-2);
end;
for y:=1 to m do
begin
readln(f,q,p);
if mat[q,0]=0 then q:=0
else
begin
j:=1;
i:=1;
if (i and p)=i then
if mat[q,0]<i then q:=0
else q:=mat[q,j];
repeat
i:=i+i;
j:=j+1;
if (i and p)=i then
if mat[q,0]<i then q:=0
else q:=mat[q,j];
until (i>=p)or(q=0);
end;
writeln(g,q);
end;
close(f);
close(g);
end.