Pagini recente » Cod sursa (job #2805133) | Cod sursa (job #712689) | Cod sursa (job #3268884) | Cod sursa (job #513503) | Cod sursa (job #1851413)
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int a[100001];
int main()
{ int n,s,d,m,ct,ant,i,j,t,x;
bool gasit;
fin>>n;
for(i=1;i<=n;i++)fin>>a[i];
//for(i=1;i<=n;i++)cout<<a[i];
fin>>m;
for(i=1;i<=m;i++)
{fin>>t>>x;
ct=0;
if(t==0)
{s=1;d=n;gasit=false;
while(s<=d&&gasit==false)
{m=(s+d)/2;
if(a[m]==x)
{for(j=m;j<=n;j++)
if(a[j]==x)ant=j;
else break;
fout<<ant;gasit=true;ct++;
}
else if(x<a[m])d=m-1;
else s=m+1;
}
if(ct==0)fout<<-1;
fout<<"\n";
}
if(t==1)
{s=1;d=n;gasit=false;
while(s<=d&&gasit==false)
{m=(s+d)/2;
if(a[m]<=x&&a[m+1]>x){fout<<m;gasit=true;}
else if(a[m]>x)d=m-1;
else s=m+1;
}
fout<<"\n";
}
if(t==2)
{s=1;d=n;gasit=false;
while(s<=d&&gasit==false)
{m=(s+d)/2;
if(a[m]>=x&&a[m-1]<x){fout<<m;gasit=true;}
else if(a[m]<x)s=m+1;
else d=m-1;
}
fout<<"\n";
}
}
fout<<"\n";
fout.close();
return 0;
}