Cod sursa(job #2224606)

Utilizator Hacks_R_UsIonescu Ioan Dorian Hacks_R_Us Data 24 iulie 2018 17:05:46
Problema Cautare binara Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#define NMAX 100010
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[NMAX];
void tip1(int n,int x)
{
    int pow2=1,pos=0;
    while(pow2<=n)
        pow2*=2;
    while(pow2>=1) {
        if(pos+pow2<=n&&v[pow2+pos]<=x)
            pos+=pow2;
        pow2>>=1;
    }
    if(v[pos]==x)
        g<<pos+1;
    else
        g<<-1;
    g<<'\n';
}
void tip2(int n,int x)
{
    int pow2=1,pos=0;
    while(pow2<=n)
        pow2*=2;
    while(pow2>=1) {
        if(pos+pow2<=n&&v[pow2+pos]<=x)
            pos+=pow2;
        pow2>>=1;
    }
    g<<pos+1;
    g<<'\n';
}
void tip3(int n,int x)
{
int pow2=1,pos=0;
    while(pow2<=n)
        pow2*=2;
    while(pow2>=1) {
        if(pos+pow2<=n&&v[pow2+pos]<x)
            pos+=pow2;
        pow2>>=1;
    }
    g<<pos+2;
    g<<'\n';
}
int main()
{
    int n,m,tip,x;
    f>>n;
    for(int i=0;i<n;i++)
        f>>v[i];
    f>>m;
    for(int i=0;i<m;i++) {
        f>>tip>>x;
        if(tip==0) tip1(n,x);
        if(tip==1) tip2(n,x);
        if(tip==2) tip3(n,x);
    }
    f.close();
    g.close();
    return 0;
}