Pagini recente » Cod sursa (job #3362074) | Cod sursa (job #3361740) | Cod sursa (job #3361587) | Cod sursa (job #3362080) | Cod sursa (job #3362069)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, m, x, q;
int v[100001];
fin>>n;
for(int i=0; i<n; i++)
fin>>v[i];
fin>>m;
while(m--)
{
fin>>q>>x;
int poz=-1;
if(q==0)
{
int st=0, dr=n-1;
//cea mai mare poz pe care o are x sau -1
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(x==v[mij])
{
poz=mij+1;
st=mij+1;
}
else if(x<v[mij])
st=mij+1;
else
dr=mij-1;
}
}
else if(q==1)
{
int st=0, dr=n-1, mij=-1;
//cea mai mare poz pe care se afla un el mai <= x
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(v[mij]<=x)
{
poz=mij+1;
st=mij+1;
}
else
dr=mij-1;
}
}
else if(q==2)
{
int st=0, dr=n-1, mij=-1;
//cea mai mica poz pe care se afla un el >=x;
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(v[mij]>=x)
{
poz=mij+1;
dr=mij-1;
}
else
st=mij+1;
}
}
fout<<poz<<'\n';
}
return 0;
}