Pagini recente » Cod sursa (job #89452) | Cod sursa (job #2699878) | Cod sursa (job #2268962) | Cod sursa (job #3266062) | Cod sursa (job #3002872)
#include<cstdio>
using namespace std;
FILE*in=fopen("cautbin.in","r");
FILE*out=fopen("cautbin.out","w");
const int NMAX=100007;
int n,m,i,v[NMAX],c,x;
int f0(int a)
{
int st=1,dr=n;
int mij,ras=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>x)
{
dr=mij-1;
}
if(v[mij]<x)
{
st=mij+1;
}
if(v[mij]==x)
{
ras=mij;
st=mij+1;
}
}
return ras;
}
int f1(int a)
{
int st=1,dr=n;
int mij,ras;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>x)
{
dr=mij-1;
}
if(v[mij]<=x)
{
ras=mij;
st=mij+1;
}
}
return ras;
}
int f2(int a)
{
int st=1,dr=n;
int mij,ras;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=x)
{
dr=mij-1;
ras=mij;
}
if(v[mij]<x)
{
st=mij+1;
}
}
return ras;
}
int main()
{
fscanf(in,"%d",&n);
for(i=1;i<=n;i++)
{
fscanf(in,"%d",&v[i]);
}
fscanf(in,"%d",&m);
for(i=1;i<=m;i++)
{
fscanf(in,"%d%d",&c,&x);
if(c==0)
{
fprintf(out,"%d\n",f0(x));
}
if(c==1)
{
fprintf(out,"%d\n",f1(x));
}
if(c==2)
{
fprintf(out,"%d\n",f2(x));
}
}
}