Pagini recente » Cod sursa (job #2554984) | Cod sursa (job #2893634) | Cod sursa (job #2718847) | Cod sursa (job #725917) | Cod sursa (job #1404584)
#include <cstdio>
using namespace std;
int N, M;
int * A;
int main()
{
freopen("cautbin.in", "r", stdin);
freopen("cautbin.out", "w", stdout);
int op, val, i, index, step;
scanf("%d", &N);
A = new int[N+5];
for (i = 0; i < N; i++)
scanf("%d", &A[i]);
scanf("%d", &M);
for (i = 0; i < M; i++)
{
scanf("%d%d", &op, &val);
for (step = 1; step < N; step <<=1);
switch (op)
{
case 0:
index = 0;
for (; step; step >>= 1)
if (index + step < N && A[index+step] <= val)
index += step;
if (A[index] == val)
printf("%d\n", index+1);
else
printf("-1\n");
break;
case 1:
index = 0;
for (; step; step >>= 1)
if (index + step < N && A[index+step] <= val)
index += step;
printf("%d\n", index+1);
break;
case 2:
index = N-1;
for (; step; step >>= 1)
if (index - step >= 0 && A[index-step] >= val)
index -= step;
printf("%d\n", index+1);
break;
}
}
return 0;
}