Pagini recente » Cod sursa (job #997566) | Cod sursa (job #769957) | Cod sursa (job #2275642) | Cod sursa (job #141076) | Cod sursa (job #274944)
Cod sursa(job #274944)
#include<stdio.h>
#define Nmax 101000
long a[Nmax],n,m;
long caut(long nr,long x)
{
long p=1,u=n,mij;
while(p<=u)
{
mij=(p+u)/2;
if(a[mij]==x)
break;
if(a[mij]>x)
u=mij-1;
else
p=mij+1;
}
mij=(p+u)/2;
switch(nr)
{
case 0: {if(a[mij]==x)
{
while(a[mij]==x && mij<=n)
mij++;
return mij-1;
}
else
return -1;
}
case 1:{if(a[mij]==x)
{ while(a[mij]==x)
mij--;
return mij+1;
}
else
if(a[mij]<x)
return mij;
else
return mij-1;
}
case 2:{if(a[mij]==x)
{ while(a[mij]==x)
mij++;
return mij-1;
}
else
if(a[mij]>x)
return mij;
else
return mij+1;
}
}
return 0;
}
int main()
{
long x=0,nr=0;
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%ld",&n);
long i;
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&m);
for(i=1;i<=m;i++)
{ scanf("%ld%ld",&nr,&x);
printf("%ld\n",caut(nr,x));
}
fclose(stdin);
fclose(stdout);
return 0;
}