Cod sursa(job #585308)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 28 aprilie 2011 21:05:31
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<cstdio>
#include<iostream>
using namespace std;
int n,m,a[100010];
int main()
{
	int i,poz,x,y;
	freopen("cautbin.in","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&m);
	freopen("cautbin.out","w",stdout);
	for(i=1;i<=m;i++)
	{
		scanf("%d %d",&x,&y);
		if(x==0)
		{
			poz=upper_bound(a+1,a+n+1,y)-a;
			poz--;
			if(a[poz]==y && poz<=n && poz>0)
				printf("%d\n",poz);
			else
				printf("-1\n");
		}
		else
			if(x==1)
			{
				poz=upper_bound(a+1,a+n+1,y)-a;
				poz--;
				printf("%d\n",poz);
			}
			else
			{
				poz=lower_bound(a+1,a+n+1,y)-a;
				if(a[poz]!=y)
					poz++;
				printf("%d\n",poz);
			}
	}
	return 0;
}