Cod sursa(job #3288529)

Utilizator Augustin_Lulelaru Augustin Augustin_ Data 22 martie 2025 16:27:54
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream o("cautbin.out");
int main()
{
	int N, M, x, acc = 0, curent_pow_2 = 16, v[100001], caz;
	f >> N;
	for (int i = 1; i <= N; i++)
	{
		f >> v[i];
	}
	f >> M;
	for (int i = 0; i < M; i++)
	{
		cout << 1;
		f >> caz >> x;
		curent_pow_2 = 16;
		acc = 0;
		while (curent_pow_2 > 0)
		{
			if (acc + curent_pow_2 <= N)
				if (v[acc + curent_pow_2] <= x)
					acc += curent_pow_2;
			curent_pow_2 >>= 1;
		}
		if (caz == 2)
		{
			acc++;
			cout << acc;
		}
		if (caz == 0)
		{
			if (v[acc] == x)
				o << acc << endl;
			else
			{
				cout << -1 << endl;
			}
		}
		if (caz == 1)
			cout<< acc << endl;
	}
	return 0;
}