Cod sursa(job #409496)

Utilizator xtephanFodor Stefan xtephan Data 3 martie 2010 18:18:35
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include<stdio.h>
#include<vector>
#include <algorithm>

using namespace std;

int n;
int v[100100];

void cit();
void go();

int main() {
	
	freopen("cautbin.in", "r", stdin);
	freopen("cautbin.out", "w", stdout);
	
	cit();
	go();
	
	return 0;
}

void cit() {
	
	scanf("%d",&n);
	for(int i=1; i<=n;i++) {
		scanf("%d", &v[i]);
	}
}


void go() {
	int m,i;
	
	sort(v+1,v+n+1);
	
	scanf("%d",&m);
	
	for(i=1; i<=m;i++) {
		
		int t,y,x;
		
		scanf("%d%d",&t,&y);
		
		if(t==0) {
			int x = upper_bound(v + 1, v + n + 1, y) - v - 1;
			
			if(x>=1 && x<=n && v[x]==y)
				printf("%d\n",x);
			else
				printf("-1");
		}
		
		if(t==1) {
			int x = lower_bound(v + 1, v + n + 1, y + 1) - v - 1;
			printf("%d\n",x);
		}
		
		if(t==2) {
			int x = upper_bound(v + 1, v + n + 1, y - 1) - v;
			printf("%d\n",x);
		}
	}
}