Cod sursa(job #1411653)

Utilizator mist.moonDenisa Gherghel mist.moon Data 31 martie 2015 21:02:02
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN=100001;

vector <int> a;
int n,m,tip,x;

int main()
{
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &x);
        a.push_back(x);
    }
    scanf("%d", &m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d", &tip, &x);
        if(tip==0) {
                int y=upper_bound(a.begin(),a.end(),x)-a.begin();
                if(1<=y and y<=n and a[y-1]==x) printf("%d \n", y);
                else printf("-1 \n");
        }
        if(tip==1) {
            int y=lower_bound(a.begin(),a.end(),x+1)-a.begin();
            printf("%d \n", y);
        }
        if(tip==2) {
            int y=upper_bound(a.begin(),a.end(),x-1)-a.begin();
            printf("%d \n", y+1);
        }
    }


    return 0;
}