Pagini recente » Cod sursa (job #1251209) | Cod sursa (job #1399213) | Cod sursa (job #428404) | Cod sursa (job #90955) | Cod sursa (job #196070)
Cod sursa(job #196070)
#include<cstdio>
long a[100000],t,i,n,x,y,c,w;
long cautbin(long st, long dr){
long mij;
if(st<=dr){
mij=st+(dr-st)/2;
if(a[mij]==x)return mij;
if(a[mij]<x)
if(a[mij+1]>x || mij==n)return mij;
else return cautbin(mij+1,dr);
if(a[mij]>x)
if(a[mij-1]<x || mij==1)return mij-1;
else return cautbin(st,mij);
}
}
int main(){
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%ld",&n);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&t);
for(;t>0;t--){
scanf("%ld %ld",&c,&x);
if(t==11376)
w=2345;
y=cautbin(1,n);
if(c==0){
while(a[y+1]==a[y])
y++;
if(a[y]==x)printf("%ld\n",y);
else printf("-1\n");
}
if(c==1){
while(a[y-1]==x)y--;
printf("%ld\n",y);
// else printf("%ld\n",y);
}
if(c==2){
while(x==a[y+1])
y++;
if(a[y]==x)printf("%ld\n",y);
else printf("%ld\n",y+1);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}