Pagini recente » Cod sursa (job #236129) | Cod sursa (job #2149104) | Cod sursa (job #2023336) | Cod sursa (job #1763971) | Cod sursa (job #874766)
Cod sursa(job #874766)
#include<iostream>
#include<fstream>
using namespace std;
int n,v[100005];
long maxstep=1<<17;
int bin0(int val)
{
long step,i,temp;
step = maxstep;
for(i=0; step>0; step>>=1)
{
temp=i+step;
if(temp<n && v[temp]<=val)
i+=step;
}
if(v[i]==val) return 1+i;
else return -1;
}
int bin1(int val)
{
long step,i,temp;
step = maxstep;
for(i=0; step>0; step>>=1)
{
temp=i+step;
if(temp<n && v[temp]<=val)
i+=step;
}
return 1+i;
}
int bin2(int val)
{
long step,i,temp;
if(v[0]>=val) return 1;
step = maxstep;
for(i=n; step>0; step>>=1)
{
temp=i-step;
if(temp>0) if(temp<n && v[temp]>=val)
i-=step;
}
return 1+i;
}
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin>>n;
for(int i=0;i<n;++i)
fin>>v[i];
int m,a,t;
fin>>m;
for( ; m; --m)
{
fin>>t>>a;
if(t==0) fout<<bin0(a)<<endl;
else if(t==1) fout<<bin1(a)<<endl;
else if(t==2) fout<<bin2(a)<<endl;
}
return 0;
}