Pagini recente » Cod sursa (job #2643724) | Cod sursa (job #652520) | Cod sursa (job #1218390) | Cod sursa (job #42917) | Cod sursa (job #342266)
Cod sursa(job #342266)
#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("%d%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("%d\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("%d\n",i+1);
break;
case 2:
for (step=1;step<N;step<<=1);
for (i=0;step;step>>=1) {
if (A[i] == x) {
while (A[i-1] == x) --i;
break;
}
else if ((i + step < N) && (A[i + step] <= x)) i+=step;
}
if (A[i] != x) printf("-1\n");
else printf("%d\n",i+1);
break;
}
}
fclose(stdin);
fclose(stdout);
return 0;
}