Cod sursa(job #229321)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 9 decembrie 2008 21:33:06
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAXN 100005

int N, M;
int v[MAXN];

int main()
{
	freopen("cautbin.in", "rt", stdin);
	freopen("cautbin.out", "wt", stdout);

	scanf("%d", &N);
	for (int i = 0; i < N; i++)
		scanf("%d", v + i);

	scanf("%d", &M);
	for (int i = 0; i < M; i++)
	{
		int type, val;
		scanf("%d %d", &type, &val);

		if (type == 0)
		{
			int poz = lower_bound(v, v + N, val) - v;
			printf("%d\n", (v[poz] == val) ? (poz + 1) : -1);
		}
		if (type == 1)
			printf("%d\n", upper_bound(v, v + N, val) - v);
		if (type == 2)
			printf("%d\n", lower_bound(v, v + N, val) - v + 1);
	}

	return 0;
}