Pagini recente » Cod sursa (job #1856258) | Cod sursa (job #1382225) | Cod sursa (job #3191454) | Cod sursa (job #3294556) | Cod sursa (job #2495124)
#include <fstream>
using namespace std;
ifstream fin("cautbin.in",ios::in);
ofstream fout("cautbin.out",ios::out);
int MULTIME[100005];
int main()
{
int N,i,a1,a2,k=0;
fin >> N;
for(i=1; i<=N; ++i)
{
fin>>MULTIME[i];
}
int st=1,med,dr=N;
int M;
fin >>M;
for(i=1; i<=M; i++)
{
st=1;
dr=N;
k=0;
fin >>a1>>a2;
med=(st+dr)/2;
while(MULTIME[med]!=a2 || MULTIME[med+1]==a2)
{
if(MULTIME[med]<a2)
{
st=med;
}
if(MULTIME[med]>a2)
{
dr=med;
}
if(a1==2&&MULTIME[med]==a2)
{
while(MULTIME[med-1]==MULTIME[med]) med=med-1;
break;
}
med=(st+dr)/2;
if(a2<MULTIME[med+1]&&a2>MULTIME[med])
{
k++;
break;
}
if((MULTIME[med]==a2&&MULTIME[med+1]==a2)||(med==(dr-1)&&MULTIME[med+1]==a2)) med++;
}
if(a1==0)
{
if(k!=0)
{
fout<<"-1"<<endl;
}else
fout <<med<<endl;
}
if(a1==1)
{
fout<<med<<endl;
}
if(a1==2)
{
if(k!=0)
{
fout<<med+1<<endl;
}else
fout<<med<<endl;
}
}
return 0;
}