Cod sursa(job #679693)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 13 februarie 2012 17:26:06
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.13 kb
var v:array [0..18, 0..250000] of longint;
    i, j, n, m, x, y, z, t:longint;
    f, g:text;
    ok:boolean;
    c:char;
    buf1, buf2:array [1.. 1 shl 17] of char;

procedure citire;
  begin
  x:=0;
  while i<n do
    begin
    read (f, c);
    if (ord(c) >= 48) and (ord(c)<=57) then begin x:=x*10+ord(c)-48; end
                                       else begin inc (i); v[1, i]:=x; x:=0; end;

    end;
  end;


begin
assign (f, 'stramosi.in');settextbuf (f, buf1); reset (f);
assign (g, 'stramosi.out'); settextbuf (g, buf2); rewrite (g);

readln (f, n, m);
i:=0;
citire;

ok:=true; i:=1;
while ok do
  begin
  ok:=false;
  for j := 1 to n do
    begin
    if v[i, j]<>0 then
      begin
      v[i+1, j]:=v[i, v[i, j]];
      if v[i+1, j] <> 0 then ok:=true;
      end;
    end;

  inc (i);
  end;

for i := 1 to m do
  begin
  readln (f, x, y);
  j:=1;
  while (x<>0) and (y<>0) do
    begin
    while y mod 2 = 0 do
      begin
      y := y div 2;
      inc (j);
      end;

    x:=v[j, x];
    y:=y div 2; inc (j);

    end;
  writeln (g, x);
  end;

close (f); close (g);
end.