Pagini recente » Cod sursa (job #1059795) | Cod sursa (job #2096672) | Cod sursa (job #2556298) | Cod sursa (job #1843680) | Cod sursa (job #398700)
Cod sursa(job #398700)
#include<stdio.h>
const int N=100001;
int n,m,v[N];
int caut( int x )
{
int i,step;
for( step=1 ; step<n ; step<<=1 );
for( i=0 ; step ; step>>=1 )
if( i+step<=n && v[i+step]<=x )
i+=step;
return i;
}
void read()
{
int x,y,z;
scanf("%d",&n);
for( int i=1 ; i<=n ; ++i )
scanf("%d",&v[i]);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&x,&y);
z=caut(y);
if(x==0)
{
if( v[z]==y )
printf("%d\n",z);
else
printf("-1\n");
}
else if( x==1 )
printf("%d\n",z);
else
{
if( v[z]<y )
++z;
else
while(v[z-1]==y )
--z;
printf("%d\n",z);
}
}
}
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
read();
return 0;
}