Cod sursa(job #15143)

Utilizator Programmer01Mierla Laurentiu Marian Programmer01 Data 10 februarie 2007 21:50:22
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.23 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..300001;
    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;
mat[1,q]:=a[p-1];
i:=1;
j:=1;
repeat
j:=j+1;
i:=i+(i xor (i-1)) and i;
mat[j,q]:=a[p-i];
until p-i<1;
mat[0,q]:=i div 2;
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);
mat[x,0]:=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);
mat[x,0]:=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[mat[x,0],q];
end;
writeln(g,q);
end;
close(g);
close(f);
end.