Pagini recente » Cod sursa (job #393071) | Cod sursa (job #1185589) | Cod sursa (job #299737) | Cod sursa (job #1279970) | Cod sursa (job #1646046)
#include <fstream>
using namespace std;
long long int n,v[100005],i,m,index,c1,x;
long long int cautbin(long long int x,long long 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;
}
}