Pagini recente » Cod sursa (job #2071092) | Cod sursa (job #2912242) | Cod sursa (job #2870445) | Cod sursa (job #172398) | Cod sursa (job #1654549)
#include <fstream>
using namespace std;
int n,v[100005],i,m,index,c1,x;
int cautbin(int x,int sfarsit)
{
int step=1, start=0;
for(; step<=sfarsit; step<<=1);
for(;step;step>>=1)
{
int index=start+step;
if(index>sfarsit) continue;
if(v[index]<=x) start=index;
}
return start;
}
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
f>>m;
for(i=1;i<=m;i++)
{
f>>c1>>x;
if(c1==0)
{
if(v[cautbin(x,v[n])]==x)
g<<cautbin(x,v[n])<<endl;
else g<<"-1"<<endl;
}
if(c1==1)
{
g<<cautbin(x,v[n])<<endl;
}
if(c1==2)
{
g<<cautbin((x-1),v[n])+1<<endl;
}
}
}