Pagini recente » Cod sursa (job #1478511) | Cod sursa (job #560286) | Cod sursa (job #476559) | Cod sursa (job #1391945) | Cod sursa (job #851991)
Cod sursa(job #851991)
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[100001];
int n;
void caut1(int x)
{
int st=1,dr=n;
int mij;
mij=st+(dr-st)/2;
while(st<=dr&&v[mij]!=x)
{
if(v[mij]<x)st=mij+1;
else dr=mij-1;
mij=st+(dr-st)/2;
}
if(st>dr)fout<<-1<<"\n";
else
{
while(mij<n&&v[mij+1]==x)mij++;
fout<<mij<<"\n";
}
}
void caut2(int x)
{
int st=1,dr=n;
int mij;
mij=st+(dr-st)/2;
while(st<=dr&&v[mij]!=x)
{
if(v[mij]<x)st=mij+1;
else dr=mij-1;
mij=st+(dr-st)/2;
}
if(st>dr)fout<<v[dr]<<"\n";
else
{
while(mij<n&&v[mij+1]==x)mij++;
fout<<mij<<"\n";
}
}
void caut3(int x)
{
int st=1,dr=n;
int mij;
mij=st+(dr-st)/2;
while(st<=dr&&v[mij]!=x)
{
if(v[mij]<x)st=mij+1;
else dr=mij-1;
mij=st+(dr-st)/2;
}
if(st>dr)fout<<v[st]<<"\n";
else
{
while(mij>1&&v[mij-1]==x)mij--;
fout<<mij<<"\n";
}
}
int main()
{
int m,i,p,x;
fin>>n;
for(i=1;i<=n;i++)
fin>>v[i];
fin>>m;
for(i=1;i<=m;i++)
{
fin>>p;
fin>>x;
if(p==0)caut1(x);
if(p==1)caut2(x);
if(p==2)caut3(x);
}
return 0;
}