Pagini recente » Cod sursa (job #317730) | Istoria paginii runda/oni2018baraj | Cod sursa (job #489387) | Cod sursa (job #251035) | Cod sursa (job #2646834)
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n, v[100010];
int cb(int a, int b)
{
int loc, step;
for(step = 1; step<=n; step<<=1);
if(a==0)
{
for(loc = 1; step; step>>=1)
{
if(loc+step <= n && v[loc+step] <= b)
loc+=step;
}
if(loc) return loc;
else return -1;
}
if(a==1)
{
for(loc = 1; step; step >>= 1)
if(loc+step <= n && v[loc+step] <= b)
loc+=step;
return loc;
}
if(a==2)
{
for(loc = n; step; step>>=1)
if(loc-step >0 &&v[loc-step] >= b)
loc-=step;
return loc;
}
}
int main()
{
f>>n;
for(int i=1;i<=n;++i)
f>>v[i];
int t;
f>>t;
int a, b;
for(int i=0; i<t; ++i)
{
f>>a>>b;
g<<cb(a,b)<<'\n';
}
}