Pagini recente » Cod sursa (job #3217618) | Cod sursa (job #2361819) | Cod sursa (job #158226) | Cod sursa (job #3152135) | Cod sursa (job #226919)
Cod sursa(job #226919)
program cautabin;
var A:array[1..100000] of longint;
n,m: longint;
k: byte;
x: longint;
i,j: longint;
function cb1(s,d,x:longint):longint;
var m: longint;
begin
cb1:=-1;
while (s<=d) do
begin
m:=s+(d-s) div 2;
if A[m]>x then d:=m-1
else
if A[m]<x then s:=m+1
else
begin
cb1:=m;
exit;
end;
end;
end;
function cb2(s,d,x:longint):longint;
var m,l: longint;
begin
l:=0;
while (s<=d) do
begin
m:=s+(d-s) div 2;
if A[m]<=x then
begin
l:=m;
s:=m+1;
end
else d:=m-1;
end;
cb2:=l;
end;
function cb3(s,d,x:longint):longint;
var m,l: longint;
begin
l:=n+1;
while (s<=d) do
begin
m:=s+(d-s) div 2;
if A[m]>=x then
begin
l:=m;
d:=d-1;
end
else
s:=m+1;
end;
cb3:=l;
end;
begin
assign(input,'cautbin.in');
reset(input);
assign(output,'cautbin.out');
rewrite(output);
read(n);
for i:=1 to n do
read(A[i]);
read(m);
for i:=1 to m do
begin
read(k,x);
if k=0 then
writeln(cb1(1,n,x))
else
if k=1 then
writeln(cb2(1,n,x))
else
writeln(cb3(1,n,x));
end;
close(input);
close(output);
end.