Pagini recente » Cod sursa (job #385306) | Cod sursa (job #1395678) | Cod sursa (job #1153829) | Cod sursa (job #2767380) | Cod sursa (job #1733399)
#include <iostream>
#include <fstream>
using namespace std;
long n,m,v[100002],y=0;
ifstream f ("cautabin.in");
ofstream t ("cautabin.out");
ifstream u ("cautabin.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)
cout<<binary_search_up(x)<<'\n';
else if (q==0){
if (v[binary_search_up(x)]!=x) cout<<-1<<'\n';
else cout<<binary_search_up(x);}
else if (q==2) cout<<binary_search_low(x)<<'\n';
}
return 0;
}