Pagini recente » Cod sursa (job #3002791) | Cod sursa (job #913393) | Cod sursa (job #1198503) | Cod sursa (job #2606847) | Cod sursa (job #195939)
Cod sursa(job #195939)
#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)/2;
if(a[mij]==x)return mij;
if(a[mij]<x)
if(a[mij+1]>x)return mij;
else return cautbin(mij+1,dr);
if(a[mij]>x)
if(a[mij-1]<x)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)
if(a[y]==x)printf("%ld\n",y+1);
else printf("-1\n");
if(c==1)
if(a[y]==x || (a[y]<x && (a[y+1]>x || y==n)))printf("%ld\n",y+1);
if(c==2)
if(a[y]==x)printf("%ld\n",y+1);
else if(a[y]<x && (a[y+1]>x || y==n))printf("%ld\n",y+2);
}
fclose(stdin);
fclose(stdout);
return 0;
}