Cod sursa(job #36809)

Utilizator Programmer01Mierla Laurentiu Marian Programmer01 Data 24 martie 2007 08:23:34
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.19 kb
program p1;
type tip=0..450001;
var mat:array[0..18,0..250001] of 0..250001;
    a,b,c,s,gr:array[0..250001] of 0..250001;
    n,i,j,q,m,y,p:0..450000;
    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;
{if k=1 then
begin}
mat[1,q]:=a[p-1];
i:=1;
j:=1;
repeat
j:=j+1;
i:=i+i;
mat[j,q]:=a[p-i]
until p-i<1;
mat[0,q]:=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);
readln(f,n,m);
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;
end;
for i:=0 to n do
begin
b[c[a[i]]]:=i;
c[a[i]]:=c[a[i]]+1;
end;
c[0]:=0;
a[0]:=0;
parc(0);
readln(f);
for y:=1 to m do
begin
readln(f,q,p);
j:=1;
i:=1;
if (i and p)=i then
if mat[0,q]<i then q:=0
else q:=mat[j,q];
repeat
i:=i+i;
j:=j+1;
if (i and p)=i then
if mat[0,q]<i then q:=0
else q:=mat[j,q];
until (i>=p)or(q=0);
writeln(g,q);
end;
close(f);
close(g);
end.