Cod sursa(job #229906)

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

int n, m;
vector<int> v;

int main()
{
	freopen("cautbin.in","r",stdin);
	freopen("cautbin.out","w",stdout);
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
	{
		int x; scanf("%d", &x);
		v.push_back(x);
	}
	scanf("%d", &m);
	for (; m; --m)
	{
		int x, y;scanf("%d%d", &x, &y);
		if (x == 1)
			printf("%d\n", lower_bound(v.begin(), v.end(), y) - v.begin());
		else if (x == 2)
			printf("%d\n", upper_bound(v.begin(), v.end(), y) - v.begin() + 1);
		else
		{
			int low = lower_bound(v.begin(), v.end(), y) - v.begin();
			if (v[low] == y) printf("%d\n", low + 1);
			else printf("-1\n");
		}
	}
	return 0;
}