Pagini recente » Cod sursa (job #1950469) | Cod sursa (job #883334) | Cod sursa (job #2322879) | Cod sursa (job #2970087) | Cod sursa (job #1343352)
#include <iostream>
#include <cstdio>
using namespace std;
#define NMAX 100000
int v[NMAX], n;
int bsearch01(int key){
int i, step;
for(step = 1;step < n;step <<= 1);
for(i = 0; step; step >>= 1){
if(i + step <= n && v[i + step] <= key)
i += step;
}
return i;
}
int bsearch2(int key){
int i, step;
for(step = 1; step < n; step <<= 1);
for(i = n; step; step >>= 1){
if(i - step > 0 && v[i - step] >= key)
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", v+ i);
int t, a, x;
scanf("%d", &t);
for(;t;--t){
scanf("%d%d", &a, &x);
if(a < 2){
int i = bsearch01(x);
if(a == 0 && v[i] != x)
printf("-1\n");
else printf("%d\n",i);
}else{
printf("%d\n",bsearch2(x));
}
}
return 0;
}