Pagini recente » Cod sursa (job #3251191) | Cod sursa (job #1438906) | Cod sursa (job #3255358) | Cod sursa (job #1205739) | Cod sursa (job #1083180)
#include<fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[100001],n,m,x,y,i;
int bin1(int y)
{
int st=1, dr=n, m, poz;
//1 3 3 3 5
poz = 0;
while(st<=dr)
{
m = (st+dr) /2;
if (a[m]<=y)
{
poz = m;
st=m+1;
}
else dr = m - 1;
}
return poz;
}
int bin0(int y)
{
int st=1, dr=n, m, poz=-1;
while(st<=dr && poz<0)
{
m = (st+dr) /2;
if (a[m]==y) poz=m;
else if (a[m]<y)
st=m+1;
else dr = m - 1;
}
if (poz>0)
while(a[poz+1]==y) poz++;
return poz;
}
int bin2(int y)
{
int st=1, dr=n,m, poz=n+1;
while(st<=dr)
{
m = (st+dr)/2;
if (a[m]>=y)
{
poz=m;
dr=m-1;
}
else st=m+1;
}
return poz;
}
int main()
{
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
f>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
switch(x)
{
case 0:
{g<<bin0(y)<<"\n"; break;}
case 1:
{g<<bin1(y)<<"\n"; break;}
case 2:
{g<<bin2(y)<<"\n"; break;}
}
}
}