Pagini recente » Cod sursa (job #207847) | Profil StarGold2 | Cod sursa (job #2196987) | Diferente pentru home intre reviziile 185 si 186 | Cod sursa (job #1094764)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
const int Nmax = 1e5 + 1e2;
int N, M, LG, V[Nmax];
int main()
{
fin >> N;
for(int i = 1; i <= N; i++)
fin >> V[i];
for(LG = 1; LG <= N; LG <<= 1);
fin >> M;
while(M--)
{
int tip, x, i, lg;
fin >> tip >> x;
if(tip < 2)
{
for(i = 0, lg = LG; lg; lg >>= 1)
if(i + lg < N && V[i + lg] <= x)
i += lg;
if(!tip && V[i] != x)
fout << "-1";
else
fout << i;
}
else
{
for(i = N, lg = LG; lg; lg >>= 1)
if(i - lg > 0 && V[i - lg] >= x)
i -= lg;
fout << i;
}
fout << '\n';
}
return 0;
}