Cod sursa(job #1205975)

Utilizator buletevladBulete Vlad buletevlad Data 8 iulie 2014 16:06:27
Problema Cautare binara Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 1.16 kb
uses crt;

var v:array[1..100000] of longint;
	x,tip,m,j,n,i:longint;
	g,f:text;

function caut0(x:longint):longint;
var pas, lim : longint;
begin
	pas:=1 shl 16;
	lim:=0;
	while pas<>0 do 
		begin
			if (lim+pas<=n) and ( v[lim+pas]<=x) then 
				lim:=lim+pas;
			pas:=pas>>1;
		end;
	if v[lim] <> x then lim := -1;
	caut0:=lim;
end;

function caut1(x:longint):longint;
var pas, lim : longint;
begin
	pas:=1<<16;
	lim:=0;
	while pas<>0 do 
		begin
			if (lim+pas<=n) and ( v[lim+pas]<=x) then 
				lim:=lim+pas;
			pas:=pas>>1;
		end;
	caut1:=lim;
end;

function caut2(x:longint):longint;
var pas, lim : longint;
begin
	pas:=1<<16;
	lim:=0;
	while pas<>0 do 
		begin
			if (lim+pas<=n) and ( v[lim+pas]<x) then 
				lim:=lim+pas;
			pas:=pas>>1;
		end;
	caut2:=lim+1;
end;

begin
	assign(f,'cautbin.in');
	reset(f);
	assign(g,'cautbin.out');
	rewrite(g);
	read(f,n);
	for i:=1 to n do 
		read(f,v[i]);
	read(f,m);
	for i:=1 to m do 
		begin
			read(f,tip,x);
			if tip = 0 then writeln(g,caut0(x));
			if tip = 1 then writeln(g,caut1(x))
				else if tip = 2 then writeln(g,caut2(x));
		end;
	close(f);
	close(g);
end.