Cod sursa(job #3375836)

Utilizator Amy_mariaBalau Amalia Amy_maria Data 14 noiembrie 2026 09:41:25
Problema Cautare binara Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[100001];
int main()
{
	int n, x, upper, p, tip;
	fin>>n;
	for(int i=1; i<=n; i++)
		fin>>v[i];
	fin>>p;
	for(int i=1; i<=p; i++)
	{
		fin>>tip >> x;
		if(tip==0)
		{
			upper = upper_bound(v+1,v+n+1,x) - v - 1;
			if(upper<n && v[upper]==x)
				fout<< upper << '\n';
			else
				fout<< -1 << '\n';
		}
		else if(tip==1)
			fout<< upper_bound(v+1,v+n+1,x) - v - 1 << '\n';
		else
			fout<< lower_bound(v+1,v+n+1,x) - v<< '\n';
	}
	return 0;
}