Cod sursa(job #2311032)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 2 ianuarie 2019 15:44:27
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;

inline char getChar(){
    poz++;
    if(poz>=LIM){
    	fread(BUF,LIM,1,stdin);
    	poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

int v[100001];

int verif(int q,int rez,int x)
{
    if(q<2)
        return v[rez]<=x;
    return v[rez]<x;
}

int main()
{
    int n,i,m,q,x,pas,rez;
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    n=getNr();
    for(i=1; i<=n; i++)
        v[i]=getNr();
    m=getNr();
    while(m--)
    {
        q=getNr();
        x=getNr();
        pas=1<<16;
        rez=0;
        while(pas)
        {
            if(rez+pas<=n && verif(q,rez+pas,x))
                rez+=pas;
            pas/=2;
        }
        switch(q)
        {
            case 0: if(v[rez]==x) printf("%d\n",rez);
                    else printf("-1\n");
                    break;
            case 1: printf("%d\n",rez);
                    break;
            case 2: printf("%d\n",rez+1);
                    break;
        }
    }

    return 0;
}