Pagini recente » Cod sursa (job #2572079) | Cod sursa (job #167568) | Cod sursa (job #2360150) | Istoria paginii teorema-chineza-a-resturilor | Cod sursa (job #1539980)
#include <fstream>
#include <algorithm>
#define DIM 100002
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int A[DIM], N, M, x, task, position;
int main()
{
fin >> N;
for(int i = 1; i <= N; i ++)
{
fin >> A[i];
}
fin >> M;
for(int i = 1; i <= M; i ++)
{
fin >> task >> x;
if(task == 0)
{
position = upper_bound(A + 1, A + N + 1, x) - A - 1;
if (position <= N && position >= 1 && A[position] == x)
{
fout << position << '\n';
}
else
{
fout << "-1\n";
}
}
else
{
if(task == 1)
{
fout << lower_bound(A + 1, A + N + 1, x + 1) - A - 1 << '\n';
}
else
{
fout << upper_bound(A + 1, A + N + 1, x - 1) - A << '\n';
}
}
}
return 0;
}