Cod sursa(job #2428771)

Utilizator Arteni_CristiArteni Cristi Arteni_Cristi Data 6 iunie 2019 13:17:09
Problema Cautare binara Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.83 kb
var t:array[1..100005] of longint;
    n,m,i,a,b,c,p,z1,z2,z3,x:longint;
begin
//assign(input,'cautbin.in'); reset(input);
//assign(output,'cautbin.out'); rewrite(output);
readln(n);
for i:=1 to n do read(t[i]);
readln(m);
for i:=1 to m do
 begin
  readln(p,x);
  a:=1; b:=n;
  while a<=b do
   begin
    c:=(a+b) div 2;
    if p=0 then
     begin
      if t[c]=x then z1:=c;
      if x<=t[c] then a:=c+1 else b:=c-1
     end;
    if p=1 then
     begin
      if t[c]<=x then z2:=c;
      if x<=x then a:=c+1 else b:=c-1
     end;
    if p=2 then
     begin
      if t[c]>=x then z3:=c;
      if t[c]>=x then b:=c-1 else a:=c+1
     end
   end;
  if p=0 then
   begin
    if z1=0 then writeln(-1) else writeln(z1)
   end;
  if p=1 then writeln(z2);
  if p=2 then writeln(z3)
 end;
close(input);
close(output)
end.