Cod sursa(job #2868961)

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

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

class InP {

	FILE *fin;
	char *buff;
	int sp;
public:
	InP(const char *p) {
		fin = fopen(p, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	char read_ch() {
		sp++;
		if (sp == 4096) {
			fread(buff, 1, 4096, fin);
			sp = 0;
		}
		return buff[sp];
	}

	InP& operator >> (int &n) {

		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');

		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}

	InP& operator >> (ll &n) {

		char c;
		ll sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');

		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}


};




class OuP {

	FILE *fout;
	char *buff;
	int sp;

public:
	OuP(const char *p) {
		fout = fopen(p, "w");
		buff = new char[50000]();
		sp = 0;
	}
	~OuP() {
		fwrite(buff, 1, sp, fout);
		fclose(fout);
	}

	void write_ch(char c) {
		if (sp == 50000) {
			fwrite(buff, 1, sp, fout);
			sp = 0;
		}
		buff[sp++] = c;
	}

	OuP& operator << (int n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*this << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP& operator << (ll n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*this << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(char c) {
		write_ch(c);
		return *this;
	}
	OuP &operator << (const char *p) {
		while (*p) {
			write_ch(*p);
			p++;
		}
		return *this;
	}

};


// InP fin("inversmodular.in");
// OuP fout("inversmodular.out");

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

int n, m;
int a[100005];

int cb(int x) {
	int st, dr, mid, ans;
	st = 1;
	dr = n;
	ans = -1;
	while (st <= dr) {
		mid = (st + dr) >> 1;
		if (a[mid] == x) {
			st = mid + 1;
			ans = mid;
		}
		else if (a[mid] >  x)
			dr = mid - 1;
		else st = mid + 1;
	}
	return ans;
}

int cb1(int x) {
	int st, dr, mid, ans;
	st = 1;
	dr = n;
	ans = -1;
	while (st <= dr) {
		mid = (st + dr) >> 1;
		if (a[mid] <= x) {
			st = mid + 1;
			ans = mid;
		}
		else if (a[mid] >  x)
			dr = mid - 1;
	}
	return ans;
}

int cb2(int x) {
	int st, dr, mid, ans;
	st = 1;
	dr = n;
	ans = -1;
	while (st <= dr) {
		mid = (st + dr) >> 1;
		if (a[mid] >= x) {
			dr = mid - 1;
			ans = mid;
		}
		else st = mid + 1;
	}
	return ans;
}

int main() {

	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> a[i];
	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';
	}

	// fin.close();
	// fout.close();
	return 0;
}