Cod sursa(job #524291)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 20 ianuarie 2011 21:41:08
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define file_in "cautbin.in"
#define file_out "cautbin.out"

int N,i,V[101001],poz,tip,x,Q;

int main(){
	
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d", &N);
	for (i=1;i<=N;++i)
		 scanf("%d", &V[i]);
	scanf("%d", &Q);
	
	while(Q--){
		
		scanf("%d %d", &tip, &x);
		
		if (tip==0){
			poz=upper_bound(V+1,V+N+1,x)-V-1;
			if (poz>=1 && poz<=N && V[poz]==x)
				 printf("%d\n", poz);
			else
				printf("-1\n");
			continue;
		}
		if (tip==1){
			poz=lower_bound(V+1,V+N+1,x+1)-V-1;
			printf("%d\n", poz);
			continue;
		}
		if (tip==2){
			poz=upper_bound(V+1,V+N+1,x-1)-V;
			printf("%d\n", poz);
		    continue;
		}
	}
	
	return 0;
	
}