Pagini recente » Cod sursa (job #1286360) | Cod sursa (job #1908865) | Cod sursa (job #1996860) | Cod sursa (job #1370729) | Cod sursa (job #1606320)
#include <iostream>
#include <cstdio>
#define MAXN 100050
using namespace std;
int a[MAXN], n, m;
int caut1(int nr)
{
int step, i;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step <= n && a[i+step] <= nr)
i += step;
return i;
}
int caut2(int nr)
{
int step, i;
for (step = 1; step < n; step <<= 1);
for (i = n; step; step >>= 1)
if (i - step >= 1 && a[i-step] >= nr)
i -= step;
return i;
}
int main()
{
freopen("cautbin.in", "r", stdin);
freopen("cautbin.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d %d", &x, &y);
if (x <= 1) {
int rez = caut1(y);
if (x == 0 && a[rez] != y)
printf("-1\n");
else
printf("%d\n", rez);
}
else
printf("%d\n", caut2(y));
}
return 0;
}