Pagini recente » Cod sursa (job #3235197) | Cod sursa (job #2552180) | Cod sursa (job #2798297) | Cod sursa (job #311777) | Cod sursa (job #313236)
Cod sursa(job #313236)
#include <stdio.h>
int n,a[100000],op,m,x,k;
int bs(int x)
{
int i,step;
for(step=1; step<=n; step<<=1);
for(i=0; step; step>>=1)
if(i+step<=n && a[i+step]<=x) i+=step;
return i;
}
int bs1(int x)
{
int i,step;
for(step=1; step<=n; step<<=1);
for(i=n; step; step>>=1)
if(i-step>0 && a[i-step]>=x) i-=step;
return i;
}
int main()
{
int il
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",&m);
for(; m>0; m--)
{
scanf("%d %d",&op,&x);
if(op<2)
{
k=bs(x);
if(op==0 && a[k]!=x) printf("-1\n");
else printf("%d\n",k);
}
else printf("%d\n",bs1(x));
}
return 0;
}