Cod sursa(job #2098221)

Utilizator dragos.galeteanu2001Dragos Iulian dragos.galeteanu2001 Data 2 ianuarie 2018 16:13:07
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#define limit 100005

using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");

int n, m, v[limit], pas;

int main()
{
	int i, x, y, j, aux;

	f >> n;
    for (i = 1; i <= n; ++i) f >> v[i];

	for (pas = 1; pas <= n; pas <<= 1);

	f >> m;

	for (j = 1; j <= m; ++j) {

		f >> x >> y;

		if (x < 2) {
			for (i = 0, aux = pas; aux; aux >>= 1)
				if (i + aux <= n && v[i + aux] <= y) i += aux;

			if (!x && v[i] != y) g << "-1" << '\n';
			else g << i;

			continue;
		}

		for (i = n, aux = pas; aux; aux >>= 1)
			if (i - aux >= 1 && v[i - aux] >= y) i -= aux;

		g << i << '\n';
	}
	f.close();
	g.close();
    return 0;
}