Pagini recente » Cod sursa (job #293430) | Cod sursa (job #3277744) | Cod sursa (job #1228077) | Cod sursa (job #155499) | Cod sursa (job #1596339)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
const int nmax = 100005;
int v[nmax], n;
void bsearch0(int val)
{
int st=1, dr=n, mid;
while(st<=dr)
{
mid=(st+dr)/2;
if(v[mid]<=val) st=mid+1;
else dr=mid-1;
}
if(v[dr]==val) fout << dr << "\n";
else fout << "-1 ";
}
void bsearch1(int val)
{
int st=1, dr=n, mid;
while(st<=dr)
{
mid=(st+dr)/2;
if(v[mid]<=val) st=mid+1;
else dr=mid-1;
}
fout << dr << "\n";
}
void bsearch2(int val)
{
int st=1, dr=n, mid;
while(st<=dr)
{
mid=(st+dr)/2;
if(v[mid]>=val) dr=mid-1;
else st=mid+1;
}
fout << st << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
int i, op, x, m;
fin >> n;
for(i=1; i<=n; i++)
fin >> v[i];
fin >> m;
for(i=1; i<=m; i++)
{
fin >> op >> x;
if(op==0) bsearch0(x);
else if(op==1) bsearch1(x);
else bsearch2(x);
}
fin.close();
fout.close();
return 0;
}