Pagini recente » Cod sursa (job #655898) | Cod sursa (job #686508) | Cod sursa (job #972400) | Cod sursa (job #786441) | Cod sursa (job #3321776)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[100001];
int cb0(int v[], int a, int n)
{
int st=1, dr=n, mij, rez=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==a)
{
rez=mij;
st=mij+1;
}
else if(v[mij]<a)
st=mij+1;
else
dr=mij-1;
}
return rez;
}
int cb1(int v[], int a, int n)
{
int st=1, dr=n, mij, rez=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=a)
{
rez=mij;
st=mij+1;
}
else
dr=mij-1;
}
return rez;
}
int cb2(int v[], int a, int n)
{
int st=1, dr=n, mij, rez=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=a)
{
rez=mij;
dr=mij-1;
}
else
st=mij+1;
}
return rez;
}
int main()
{
int n, m, cer, a, ok=-1000;
fin >> n;
for(int i=1; i<=n; i++)
{
fin >> v[i];
}
fin >> m;
for(int i=1; i<=m; i++)
{
fin >> cer >> a;
if(cer==0)
{
ok=cb0(v, a, n);
fout << ok << "\n";
}
else if(cer==1)
{
fout << cb1(v, a, n) << "\n";
}
else
{
fout << cb2(v, a, n) << "\n";
}
//fout << "\n" << "\n";
}
return 0;
}