Cod sursa(job #1026362)

Utilizator vlcmodanModan Valentin vlcmodan Data 11 noiembrie 2013 15:49:00
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[100001],n,i,j,x,y,t,m;
int main()
{
	freopen("cautbin.in","r",stdin);
	freopen("cautbin.out","w",stdout);
	scanf("%d",&n);

	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&m);
	for(i=1;i<=m;i++)
	{
		scanf("%d %d",&t,&x);
		if(t==0)
			{
					y=upper_bound(a,a+n+1,x)-a-1;
				 if (y<=n&&y>=1&&a[y]==x)
					printf("%d\n",y);
				else
					printf("-1\n");
			}
		else 
		if (t == 1) {
            y = lower_bound(a,a+n+1,x+1)-a-1;
            printf("%d\n", y);
        } else {
            y = upper_bound(a,a+n+1,x-1)-a;
            printf("%d\n", y);

		}
	}
	return 0;}