Cod sursa(job #229319)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 9 decembrie 2008 21:32:25
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 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;
			if (v[poz] == val)
				printf("%d\n", poz + 1);
			else
				printf("-1\n");
		}
		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;
}