Pagini recente » Cod sursa (job #2914958) | Cod sursa (job #1659193) | Cod sursa (job #1751227) | Cod sursa (job #70991) | Cod sursa (job #1733400)
#include <iostream>
#include <fstream>
using namespace std;
long n,m,v[100002],y=0;
ifstream f ("cautbin.in");
ofstream t ("cautbin.out");
ifstream u ("cautbin.ok");
long binary_search_up(long val)
{long i, step;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n and v[i + step] <= val)
i += step;
return i+1;
}
long binary_search_low(long val)
{long i, step;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n and v[i + step] < val)
i += step;
return i+2;
}
void check(long solution){long response;
u>>response;
if (solution!=response)
cout<<"Error: "<<y<<'\n';
++y;
}
int main()
{long q,x;
f>>n;
for (long i=0;i<n;++i)
f>>v[i];
f>>m;
for (long i=0;i<m;++i){
f>>q>>x;
if (q==1)
t<<binary_search_up(x)<<'\n';
else if (q==0){
if (v[binary_search_up(x)]!=x) t<<-1<<'\n';
else t<<binary_search_up(x);}
else if (q==2) t<<binary_search_low(x)<<'\n';
}
return 0;
}