Cod sursa(job #15846)

Utilizator Programmer01Mierla Laurentiu Marian Programmer01 Data 11 februarie 2007 19:33:39
Problema Stramosi Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.39 kb
program p1;
type tip=0..250001;
var mat:array[0..20,0..250001] of tip;
    a,b,c,s,gr:array[0..250001] of tip;
    n,i,j,q:tip;
    m,x,y,p:0..350001;
    f,g:text;
procedure parc(vf:tip);
var k:tip;
begin
for k:=1 to gr[vf] do
begin
q:=b[s[vf]+k-1];
c[q]:=c[vf]+1;
p:=c[q];
a[p]:=q;
for i:=1 to p do
write(a[i],' ');
writeln;;
if k=1 then
begin
mat[q,1]:=a[p-1];
i:=1;
j:=1;
repeat
j:=j+1;
i:=i+(i xor (i-1)) and i;
mat[q,j]:=a[p-i]
until p-i<1;
mat[q,0]:=i div 2;
end
else
begin
p:=b[s[vf]];
i:=0;
while mat[i,p]<>0 do
begin
mat[i,q]:=mat[i,p];
i:=i+1;
end;
end;
parc(q);
end;
end;
begin
assign(f,'stramosi.in');
reset(f);
assign(g,'stramosi.out');
rewrite(g);
read(f,n,m);
x:=1;
for i:=1 to n do
begin
read(f,a[i]);
gr[a[i]]:=gr[a[i]]+1;
end;
y:=0;
s[0]:=1;
for i:=0 to n do
begin
y:=y+gr[i];
s[i+1]:=y+1;
c[i+1]:=y+1;
p:=a[i];
b[c[p]]:=i;
c[p]:=c[p]+1;
end;
c[0]:=0;
a[0]:=0;
parc(0);
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(g);
close(f);
end.