Pagini recente » Cod sursa (job #1422616) | Cod sursa (job #1334604) | Cod sursa (job #1353083) | Cod sursa (job #2312005) | Cod sursa (job #2427819)
#include <fstream>
using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
int v[100001];
int cb0(int st, int dr, int x)
{
int mij;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
st=mij+1;
else
dr=mij-1;
}
mij=(st+dr)/2;
if(v[mij]>x)
mij--;
if(v[mij]==x)
return mij;
return -1;
}
int cb1(int st, int dr, int x)
{
int mij,n=dr;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
st=mij+1;
else
dr=mij;
}
mij=(st+dr)/2;
if(v[mij]>x)
mij--;
return mij;
}
int cb2(int st, int dr, int x)
{
int mij;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]<x)
st=mij+1;
else
dr=mij;
}
mij=(st+dr)/2;
if(v[mij]<x)
mij++;
return mij;
}
int main()
{
int i,n,m,x,y;
in>>n;
for(i=1;i<=n;i++)
in>>v[i];
in>>m;
while(m--)
{
in>>x>>y;
if(x==0)
out<<cb0(1,n,y)<<'\n';
if(x==1)
out<<cb1(1,n,y)<<'\n';
if(x==2)
out<<cb2(1,n,y)<<'\n';
}
return 0;
}