Cod sursa(job #461419)

Utilizator hunter_ionutzzzFarcas Ionut hunter_ionutzzz Data 6 iunie 2010 18:49:18
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.22 kb
program p1;
type tip=0..250001;
var mat:array[0..18,0..250001] of 0..250001;
    n,m,i,j,p,q,x,y:0..350001;
    fin,fout:text;
begin
    assign(fin,'stramosi.in'); reset(fin);
    assign(fout,'stramosi.out'); rewrite(fout);
    read(fin,n,m);
    for i:=1 to n do
       read(fin,mat[1,i]);
       for i:=1 to n do
           begin
           j:=1;
           p:=0;
           repeat
              j:=j+1;
              x:=mat[j-1,i];
              if x=0 then p:=1
              else
                  begin
                  q:=mat[j-1,x];
                  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(fin,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(fout,q);
           end;
      close(fin);
      close(fout);
end.