Cod sursa(job #1653082)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 15 martie 2016 18:36:40
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cautbin.in");
ofstream fout("cautbin.out");

int n, m;
vector<int> v;

int main()
{
	int i, t, x;
	vector<int>::iterator it;

	fin >> n;
	for (i = 1; i <= n; i++)
	{
		fin >> x;
		v.push_back(x);
	}
	fin >> m;
	for (i = 1; i <= m; i++)
	{
		fin >> t >> x;
		if (t == 0)
		{
			it = lower_bound(v.begin(), v.end(), x + 1);
			if (*(it-1) == x)
				fout << it - v.begin() << '\n';
			else fout << -1 << '\n';
		}
		else if (t == 1)
			fout << lower_bound(v.begin(), v.end(), x + 1) - v.begin() << '\n';
		else
			fout << upper_bound(v.begin(), v.end(), x - 1) - v.begin() + 1 << '\n';
	}
	return 0;
}