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