Pagini recente » Cod sursa (job #1387542) | Cod sursa (job #841054) | Cod sursa (job #1390830) | Cod sursa (job #2936436) | Cod sursa (job #819306)
Cod sursa(job #819306)
#include<stdio.h>
int n,a[100005];
void f0(int x)
{
int st,dr,med,last=-1;
st=1;dr=n;
while(st<=dr)
{
med=(st+dr)/2;
if(a[med]==x) {last=med;st=med+1;}
else
if(a[med]>x) dr=med-1;
else st=med-1;
}
printf("%d\n",last);
}
void f1(int x)
{
int st,dr,med,last=-1;
st=1;dr=n;
while(st<=dr)
{
med=(st+dr)/2;
if(a[med]<=x) {last=med;st=med+1;}
else dr=med-1;
}
printf("%d\n",last);
}
void f2(int x)
{
int st,dr,med,last=-1;
st=1;dr=n;
while(st<=dr)
{
med=(st+dr)/2;
if(a[med]>=x) {last=med;dr=med-1;}
else st=med+1;
}
printf("%d\n",last);
}
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
int i,m,t,x;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&t,&x);
if(t==0) f0(x);
if(t==1) f1(x);
if(t==2) f2(x);
}
return 0;
}