Pagini recente » Cod sursa (job #2890437) | Cod sursa (job #1737471) | Cod sursa (job #473034) | Cod sursa (job #2002623) | Cod sursa (job #895082)
Cod sursa(job #895082)
#include <iostream>
#include <fstream>
using namespace std;
int n,m;
int v[100010],ma;
int binary_search2(int val)
{
int i,step;
for (step=ma,i = n; step; step >>= 1)
if (i - step > 0 && v[i - step] >= val)
i -= step;
return i;
}
int binary_search1(int val)
{
int i,step;
for (step=ma,i = 0; step; step >>= 1)
if (i + step <= n && v[i + step] <= val)
i += step;
return i;
}
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin>>n;
for(int i=1;i<=n;i++)
fin>>v[i];
fin>>m;
int x,y;
for (ma = 1; ma <= n; ma <<= 1);
for(int i=1;i<=m;i++)
{
fin>>x>>y;
if(x<2)
{
int p=binary_search1(y);
if(!x && v[p]!=y)
fout<<"-1\n";
else fout<<p<<"\n";
}
else fout<<binary_search2(y)<<"\n";
}
return 0;
}