Cod sursa(job #2881305)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 30 martie 2022 13:57:12
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 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 x, y;
		fin >> x >> y;
		if (x == 0) {
			int p = upper_bound(a + 1, a + n + 1, y) - a;
			if (a[p - 1] == y)
				fout << p - 1 << '\n';
			else fout << "-1\n";
		}
		else if (x == 1) {
			int p = upper_bound(a + 1, a + n + 1, y) - a;

			fout << p - 1;
		}
		else {
			int p = lower_bound(a + 1, a + n + 1, y) - a;
			fout << p << " ";

		}
	}
	return 0;
}