Pagini recente » Cod sursa (job #2918048) | Cod sursa (job #1723680) | Cod sursa (job #1575862) | Cod sursa (job #2335898) | Cod sursa (job #195943)
Cod sursa(job #195943)
#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){
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;
}