Pagini recente » Cod sursa (job #2934896) | Cod sursa (job #2424898) | Cod sursa (job #1789050) | Cod sursa (job #259049) | Cod sursa (job #226857)
Cod sursa(job #226857)
program cautabin;
var A:array[1..100000] of longint;
n,m: longint;
k,x: longint;
i: longint;
function cb1(s,d,x:longint):longint;
var m: longint;
begin
if s>d then cb1:=-1
else
begin
m:=s+(d-s) div 2;
if A[m]>x then cb1:=cb1(s,m-1,x)
else
if A[m]<x then cb1:=cb1(m+1,d,x)
else
cb1:=m;
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);
readln(n);
for i:=1 to n do
read(A[i]);
readln;
readln(m);
for i:=1 to m do
begin
readln(k,x);
case k of
0: writeln(cb1(1,n,x));
1: writeln(cb2(1,n,x));
2: writeln(cb3(1,n,x));
end;
end;
close(input);
close(output);
end.