Pagini recente » Cod sursa (job #1454787) | Cod sursa (job #216840) | Cod sursa (job #2014866) | Cod sursa (job #808280) | Cod sursa (job #342271)
Cod sursa(job #342271)
#include <iostream>
#include <cstdio>
using namespace std;
long A[100000];
long N,M;
int main() {
ios::sync_with_stdio(false);
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%ld",&N);
for (long i=0;i<N;++i) scanf("%ld",&A[i]);
long step,x,c,i;
scanf("%ld",&M);
for(;M;M--) {
scanf("%ld%ld",&c,&x);
switch (c) {
case 0:
for (step=1;step<N;step<<=1);
for (i=0;step;step>>=1) {
if ((i + step < N) && (A[i + step] <= x)) i+=step;
}
if (A[i] != x) printf("-1\n");
else printf("%ld\n",i+1);
break;
case 1:
for(step=1;step<N;step<<=1);
for (i=0;step;step>>=1) {
if ((i + step < N) && (A[i + step] <= x)) i+=step;
}
printf("%ld\n",i+1);
break;
case 2:
for(step=1;step<N;step<<=1);
for (i=N-1;step;step>>=1) {
if ((i - step >= 0) && (A[i - step] >= x)) i-=step;
}
printf("%ld\n",i+1);
break;
}
}
fclose(stdin);
fclose(stdout);
return 0;
}