Cod sursa(job #2881318)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 30 martie 2022 14:03:10
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "cautbin";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n;

int a[100005];

int main() {


	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> a[i];
	int m;
	fin >> m;
	while (m--) {
		int op, x;
		fin >> op >> x;
		if (op == 0)
			fout << (a[upper_bound(a + 1, a + n + 1, x) - a - 1] == x ? upper_bound(a + 1, a + n + 1, x) - a - 1 : -1) << '\n';
		else if (op == 1) fout << upper_bound(a + 1, a + n + 1, x) - a - 1 << '\n';
		else fout << lower_bound(a + 1, a + n + 1, x) - a << '\n';
	}
	return 0;
}