Pagini recente » Istoria paginii utilizator/cergan.naomi | Cod sursa (job #2786477) | Cod sursa (job #1205680) | Cod sursa (job #1389172) | Cod sursa (job #465223)
Cod sursa(job #465223)
program cautarebinara ;
const FIN = 'cautbin.in';
FOU = 'cautbin.out';
var N, i, T, aux, cnt, a, val : longint ;
V : array[1 .. 100005] of longint ;
f, g : text ;
begin
assign ( f, FIN ) ; reset ( f ) ;
assign ( g, FOU ) ; rewrite ( g ) ;
readln ( f, N ) ;
for i := 1 to N do
read ( f, V[i] ) ;
readln ( f ) ; readln ( f, T ) ;
aux := 1 ;
while ( aux <= N ) do
aux := aux shl 1 ;
while ( T <> 0 ) do
begin
readln ( f, a, val ) ;
if ( a <> 2 ) then
begin
cnt := aux; i := 0 ;
while ( cnt <> 0 ) do
begin
if ( i + cnt <= N ) and ( V[i + cnt] <= val ) then
inc ( i, cnt ) ;
cnt := cnt shr 1 ;
end;
if ( a = 0 ) and ( V[i] = val ) or ( a = 1 ) then
writeln ( g, i )
else writeln ( g, '-1' ) ;
end
else
begin
cnt := aux; i := 0 ;
while ( cnt <> 0 ) do
begin
if ( i + cnt <= N ) and ( V[i + cnt] < val ) then
inc ( i, cnt ) ;
cnt := cnt shr 1 ;
end;
writeln ( g, i + 1 ) ;
end;
dec ( T ) ;
end;
close ( f ) ; close ( g ) ;
end.