Cod sursa(job #629280)

Utilizator stef2nStefan Istrate stef2n Data 3 noiembrie 2011 01:04:18
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int N, M;
int x[100001];

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

	cin >> N;
	for (int i = 0; i < N; ++i)
		cin >> x[i];

	cin >> M;
	for (int i = 0; i < M; ++i) {
		int op, elem, *p;
		cin >> op >> elem;
		if (op == 0) {
			p = upper_bound(x, x + N, elem);
			if (p == x || *(p - 1) != elem)
				cout << "-1\n";
			else
				cout << (p - x) << "\n";
		} else if (op == 1) {
			p = upper_bound(x, x + N, elem);
			cout << (p - x) << "\n";
		} else {
			p = lower_bound(x, x + N, elem);
			cout << (p - x + 1) << "\n";
		}
	}
	return 0;
}