Cod sursa(job #2254351)

Utilizator IOI_MDA_003Sebastian Chicu IOI_MDA_003 Data 5 octombrie 2018 08:44:46
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;

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

const int N_MAX = 100000 + 5;
int x[N_MAX], n; 

int main() {
	fin >> n;
	for(int i = 1; i<=n; ++i)
		fin >> x[i];
	int m;
	fin >> m;
	while(m--){
		int tip, val;
		fin >> tip >> val;
		if(tip == 0){
			int poz = upper_bound(x+1, x+n+1, val) - x - 1;
			if (x[poz] == val)
				fout << poz << "\n";
			else
				fout << "-1\n"; 
		} else if (tip == 1){
			fout << upper_bound(x + 1, x + n + 1, val) - x - 1 << "\n";
		} else if(tip == 2){
			fout << lower_bound(x+1, x+n+1, val) - x << "\n";
		}
	}
	return 0;
}