Pagini recente » Cod sursa (job #2963983) | Cod sursa (job #1245507) | Cod sursa (job #3191883) | Cod sursa (job #256917) | Cod sursa (job #391949)
Cod sursa(job #391949)
const maxn=100000;
type vector=array[1..maxn]of longint;
var n,m,i,x,p,q,r,s:longint;
t:byte;
v:vector;
function caut0:longint;
begin
p:=1;
q:=n;
caut0:=-1;
repeat
r:=(p+q)div 2;
s:=v[r];
if s=x then begin
caut0:=r;
p:=r+1;
end
else if x<s then q:=r-1
else p:=r+1;
until p>q;
end;
function caut1:longint;
begin
p:=1;
q:=n;
repeat
r:=(p+q)div 2;
if x<v[r]then q:=r-1
else begin
caut1:=r;
p:=r+1;
end;
until p>q;
end;
function caut2:longint;
begin
p:=1;
q:=n;
repeat
r:=(p+q)div 2;
if x<=v[r]then begin
caut2:=r;
q:=r-1;
end
else p:=r+1;
until p>q;
end;
begin
assign(input,'cautbin.in');
reset(input);
assign(output,'cautbin.out');
rewrite(output);
read(n);
for i:=1 to n do read(v[i]);v[n+1]:=0;
read(m);
for i:=1 to m do
begin
read(t,x);
case t of
0:writeln(caut0);
1:writeln(caut1);
2:writeln(caut2);
end;
end;
close(output);
close(input);
end.