Pagini recente » Cod sursa (job #2218778) | Cod sursa (job #965927) | Cod sursa (job #844324) | Cod sursa (job #2840382) | Cod sursa (job #2679115)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int N, A[100], val, i;
int step;
int binary_search(int val)
{
int i, step;
for (step = 1; step < N; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < N && A[i + step] <= val)
i += step;
return i;
}
int lg, logN;
int aux;
int M, tip ,x;
int main()
{
f>>N;
for(i=1; i<=N; ++i)
f>>A[i];
for(logN = 1 ; logN<=N; logN<<=1)
f>>M;
for( ; M; --M)
{
f>>tip>>x;
if(tip<2)
{
aux = binary_search(M);
if(!tip and !aux)
g<<"-1"<<endl;
else if(!tip) g<<aux<<endl;
if(tip==1 and aux) g<<aux<<endl;
else g<<A[aux-1]<<endl;
continue;
}
for (lg = logN, i = N; lg; lg >>= 1)
if (i - lg > 0 && A[i - lg] >= x)
i -= lg;
g<<i<<endl;
}
return 0;
}