Cod sursa(job #1185698)

Utilizator crucerucalinCalin-Cristian Cruceru crucerucalin Data 16 mai 2014 14:04:46
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <cstdio>
#include <algorithm>

const int NMAX = 100005;
int V[NMAX];


int main()
{
	freopen("cautbin.in", "r", stdin);
	freopen("cautbin.out", "w", stdout);

	int N, M;
	int i;

	scanf("%d", &N);
	for (i = 0; i < N; ++i)
		scanf("%d", &V[i]);

	scanf("%d", &M);
	for (; M; --M) {
		int type, x;
		int ret;

		scanf("%d %d", &type, &x);
		if (type == 0) {
			int ret = std::upper_bound(V, V+N, x) - V - 1;
			if (V[ret] != x)
				printf("-1\n");
			else
				printf("%d\n", ret + 1);
		} else if (type == 1) {
			int ret = (std::lower_bound(V, V+N, x+1) - V - 1);
			printf("%d\n", ret + 1);
		} else {
			int ret = (std::upper_bound(V, V+N, x-1) - V) + 1;
			printf("%d\n", ret + 1);
		}
	}

	return 0;
}