Cod sursa(job #444257)

Utilizator batistaUPB-Oprea-Cosmin-Dumitru batista Data 19 aprilie 2010 20:58:14
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{long V[100002],i,N,M,t,x,y;
	  freopen("cautbin.in","r",stdin);
	  freopen("cautbin.out","w",stdout);
	  scanf("%ld",&N);
	 for(i=1;i<=N;i++)
 	   scanf("%ld",&V[i]);
	   scanf("%ld",&M);
	for (i = 1; i <= M; ++ i) 
	   {
         scanf("%d%d", &t, &y);
         if (t == 0) 
		  {
            x = upper_bound(V + 1, V + N + 1, y) - V - 1;
            if (x <= N && x >= 1 && V[x] == y)
              printf("%d\n", x);
             else
            printf("-1\n");
           } 
		  else if (t == 1) 
		   {
            x = lower_bound(V + 1, V + N + 1, y + 1) - V - 1;
            printf("%d\n", x);
		   } 
		  else 
		   {
            x = upper_bound(V + 1, V + N + 1, y - 1) - V;
		    printf("%d\n", x);
           }
      }
	   return 0;}