Mai intai trebuie sa te autentifici.

Cod sursa(job #17740)

Utilizator Programmer01Mierla Laurentiu Marian Programmer01 Data 16 februarie 2007 19:22:55
Problema Stramosi Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.99 kb

program p1;
type tip=0..250001;
var mat:array[0..17,0..250001] of tip;
    a,b,c,gr,s:array[0..250001] of tip;
    n,i,j,q:tip
    m,x,y,p:0..300001;
    f,g:text;
begin
assign(f,'stramosi.in');
reset(f);
assign(g,'stramosi.out');
rewrite(g);
read(f,n,m);
y:=0;
for i:=1 to n do
begin
read(f,a[i]);
mat[1,i]:=a[i];
end;
for i:=1 to n do
begin
j:=1;
p:=0;
repeat
j:=j+1;
if mat[j-1,i]=0 then p:=1
else
begin
q:=mat[j-1,mat[j-1,i]];
if q=0 then p:=1
else mat[j,i]:=q;
end;
until p=1;
mat[0,i]:=1 shl (j-2);
end;
for y:=1 to m do
begin
readln(f,q,p);
x:=0;
j:=0;
repeat
j:=j+1;
b[j]:=p mod 2;
if b[j]=1 then
begin
x:=x+1;
c[x]:=1 shl (j-1);
gr[x]:=j;
end;
p:=p div 2;
until p<=1;
if p=1 then
begin
j:=j+1;
b[j]:=p;
x:=x+1;
c[x]:=1 shl (j-1);
gr[x]:=j;
end;
x:=x+1;
if mat[0,q]=0 then q:=0
else
while x>1 do
begin
x:=x-1;
if mat[0,q]<c[x] then
begin
i:=x;
q:=0;
end
else q:=mat[gr[x],q];
end;
writeln(g,q);
end;
close(f);
close(g);
end.