Cod sursa(job #1412715)

Utilizator vladrochianVlad Rochian vladrochian Data 1 aprilie 2015 14:11:11
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

int N, M;
vector<int> v;

int main()	{
	fin >> N;
	v.resize(N);
	for (int &i : v)
		fin >> i;
	fin >> M;
	while (M--) {
		int t, val;
		fin >> t >> val;
		if (t == 0) {
			auto it = upper_bound(v.begin(), v.end(), val);
			if (it == v.begin() || *(--it) != val)
				fout << "-1\n";
			else
				fout << it - v.begin() + 1 << "\n";
		} else if (t == 1) {
			fout << upper_bound(v.begin(), v.end(), val) - v.begin() << "\n";
		} else {
			fout << lower_bound(v.begin(), v.end(), val) - v.begin() + 1 << "\n";
		}
	}
	return 0;
}