Pagini recente » Cod sursa (job #1569888) | Cod sursa (job #1734045) | Atasamentele paginii jc2019-runda-2 | Cod sursa (job #3150263) | Cod sursa (job #713046)
Cod sursa(job #713046)
#include<iostream>
#include<cstdio>
int t,i,n,x,y,a[100001];
int caut1(int l)
{
int st=1,dr=n,m,poz=0;
while(st<=dr)
{
m=(st+dr)/2;
if(a[m]==l)
{
poz=m;
st=m+1;
}
else
if(l<a[m])
dr=m-1;
else
st=m+1;
}
if(!poz) return -1;
else return poz;
}
int caut2(int l)
{
int st=1,dr=n,m,poz=0;
while(st<=dr)
{
m=(st+dr)/2;
if(l<a[m])
dr=m-1;
else
if(l==a[m])
{
st=m+1;
poz=m;
}
else st=m+1;
}
return poz;
}
int caut3(int l)
{
int st=1,dr=n,poz=0,m;
while(st<=dr)
{
m=(st+dr)/2;
if(a[m]==l)
{
poz=m;
dr=m-1;
}
else if(l<a[m])
dr=m-1;
else st=m+1;
}
return poz;
}
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&x,&y);
if(x==0)
printf("%d\n",caut1(y));
else
if(x==1)
printf("%d\n",caut2(y));
else
printf("%d\n",caut3(y));
}
return 0;
}