Pagini recente » Cod sursa (job #727578) | Cod sursa (job #2517988) | Cod sursa (job #1564274) | Cod sursa (job #260493) | Cod sursa (job #196054)
Cod sursa(job #196054)
#include<cstdio>
long a[100000],t,i,n,x,y,c;
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=0;i<n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&t);
for(;t>0;t--){
scanf("%ld %ld",&c,&x);
y=cautbin(0,n);
if(c==0){
while(a[y+1]==x)y++;
if(a[y]==x)printf("%ld\n",y+1);
else printf("-1\n");
}
if(c==1){
while(a[y]==x)y--;
if(a[y+1]==x)printf("%ld\n",y+2);
else printf("%ld\n",y+1);
}
if(c==2){
while(a[y]==x)y++;
if(a[y-1]==x)printf("%ld\n",y);
else printf("%ld\n",y+2);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}