Cod sursa(job #229907)

Utilizator ScrazyRobert Szasz Scrazy Data 12 decembrie 2008 08:16:37
Problema Cautare binara Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 100002

int n, m;
int v[MAX_N];

int main()
{
	freopen("cautbin.in","r",stdin);
	freopen("cautbin.out","w",stdout);
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", &v[i]);

	scanf("%d", &m);
	
	for (; m; --m)
	{
		int x, y;scanf("%d%d", &x, &y);
		if (x == 1)
			printf("%d\n", lower_bound(v, v+n, y) - v);
		else if (x == 2)
			printf("%d\n", upper_bound(v, v+n, y) - v + 1);
		else
		{
			int low = lower_bound(v, v+n, y) - v;
			if (v[low] == y) printf("%d\n", low + 1);
			else printf("-1\n");
		}
	}
	return 0;
}