Pagini recente » Cod sursa (job #2652260) | Cod sursa (job #1082408) | Cod sursa (job #1798578) | Cod sursa (job #2821699) | Cod sursa (job #196063)
Cod sursa(job #196063)
#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-1)return mij;
else return cautbin(mij+1,dr);
if(a[mij]>x)
if(a[mij-1]<x || mij==0)return mij-1;
else return cautbin(st,mij-1);
}
}
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]==a[y-1])y--;
if(a[y]==x)printf("%ld\n",y);
else printf("%ld\n",y-1);
}
if(c==2){
while(a[y]==a[y+1])
y++;
if(a[y]==x)printf("%ld\n",y);
else printf("%ld\n",y+1);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}