Pagini recente » Cod sursa (job #1155252) | Cod sursa (job #1715359) | Cod sursa (job #3247978) | Cod sursa (job #2597310) | Cod sursa (job #798802)
Cod sursa(job #798802)
#include <cstdio>
using namespace std;
long a[100001];
int main () {
long n,i,m,k,x,step,j;
freopen ("cautbin.in","r",stdin);
freopen ("cautbin.out","w",stdout);
scanf ("%ld",&n);
for (i=0;i<n;i++)
scanf ("%ld",&a[i]);
scanf ("%ld",&m);
for (i=1;i<=m;i++) {
scanf ("%ld%ld",&k,&x);
if (k==0) {
j=0;
for (step=1;step<n;step<<=1);
for (j=0;step;step>>=1) {
if (j+step<n && a[j+step]<=x)
j=j+step;
}
if (a[j]==x)
printf ("%ld\n",j+1);
else printf ("-1\n");
}
if (k==1) {
j=0;
for (step=1;step<n;step<<=1);
for (j=0;step;step>>=1) {
if (j+step<n && a[j+step]<=x)
j=j+step;
}
printf ("%ld\n",j+1);
}
if (k==2) {
j=0;
step=1<<16;
for (j=0;step;step>>=1) {
if (j+step<n && a[j+step]<x)
j=j+step;
}
printf ("%ld\n",j+2);
}
}
return 0;
}