Pagini recente » Cod sursa (job #2596392) | Cod sursa (job #120373) | Cod sursa (job #741434) | Cod sursa (job #1064625) | Cod sursa (job #673178)
Cod sursa(job #673178)
#include <fstream>
#include <iostream>
using namespace std;
const char iname[]="cautbin.in";
const char oname[]="cautbin.out";
ifstream f(iname);
ofstream g(oname);
const int maxn=100005;
int a[maxn], n, m, x, y;
int cb0(int val)
{
int step, i;
for(step=1; step<n; step<<=1);
for(i=1; step; step>>=1)
if(i+step<=n && a[i+step]<=val) i+=step;
if(a[i]==val) return i;
return -1;
}
int cb1(int val)
{
int step, i;
for(step=1; step<n; step<<=1);
for(i=1; step; step>>=1)
if(i+step<=n && a[i+step]<=val) i+=step;
if(a[i]<val) return i+1;
return i;
}
int cb2(int val)
{
int step, i;
for(step=1; step<n; step<<=1);
for(i=1; step; step>>=1)
if(i+step<=n && a[i+step]<val) i+=step;
if(a[i]==val) return i;
return i+1;
}
int main()
{
f>>n;
for(int i=1; i<=n; ++i) f>>a[i];
for(f>>m; m; --m)
{
f>>x>>y;
if(x==0) g<<cb0(y)<<"\n";
else if(x==1) g<<cb1(y)<<"\n";
else if(x==2) g<<cb2(y)<<"\n";
}
}