Cod sursa(job #196660)

Utilizator theratmantheratman theratman Data 27 iunie 2008 20:25:48
Problema Cautare binara Scor 40
Compilator fpc Status done
Runda Arhiva educationala Marime 0.83 kb
var f,g:text;
    a:array[1..100000] of int64;
    op,x,n,m:int64;
    i:longint;
function cautbin(st,dr:longint):longint;
 var poz,m:longint;
 begin
 poz:=0;
 while (st<=dr)and(poz=0) do begin
  m:=(st+dr) div 2;
  if a[m]=x then begin
   while x=a[m] do inc(m);
   dec(m);
   poz:=m;
  end;
  if a[m]>x then dr:=m-1
            else st:=m+1;
 end;
 m:=(st+dr)div 2;
 if op=0 then
  if poz=0 then cautbin:=-1
           else cautbin:=poz;
 if op=1 then
  if poz=0 then cautbin:=m
           else cautbin:=poz;
 if op=2 then
  if poz=0 then cautbin:=m+1
           else cautbin:=poz;
 end;
begin
assign(f,'cautbin.in');reset(f);
assign(g,'cautbin.out');rewrite(g);
read(f,n);
for i:=1 to n do read(f,a[i]);
read(f,m);
for i:=1 to m do begin
 read(f,op,x);
 writeln(g,cautbin(1,n));
end;
close(f);
close(g);
end.