Pagini recente » Cod sursa (job #2970714) | Cod sursa (job #792694) | Cod sursa (job #245586) | Cod sursa (job #1123950) | Cod sursa (job #21784)
Cod sursa(job #21784)
program p1;
type tip=0..250001;
var mat:array[0..17,0..250001] of tip;
a,b,c,s,gr:array[0..250001] of tip;
n,i,j,q,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;
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);
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;
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);
for y:=1 to m do
begin
readln(f,q,p);
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);
writeln(g,q);
end;
close(f);
close(g);
end.