Cod sursa(job #1278148)

Utilizator GinguIonutGinguIonut GinguIonut Data 28 noiembrie 2014 16:12:51
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int st,dr,ok,v[100001],i,n,m,x,z,cond;
void fct(int w)
{
   while(st!=dr&&ok==0)
    {
                    z=(st+dr)/2;
                    if(x==v[z])
                    {
                        ok=1;
                        while(x==v[z])
                        {
                            z=z-1;
                        }
                    }
                    else
                    {
                        if(x>v[z])
                           st=z+1;
                        else
                            dr=z-1;
                    }
                }
}
void fct2(int w)
{
    while(st!=dr&&ok==0)
    {
                    z=(st+dr)/2;
                    if(x==v[z])
                    {
                        ok=1;
                        while(x==v[z])
                        {
                            z--;
                        }
                    }
                    else
                    {
                        if(x>v[z])
                           st=z+1;
                        else
                            dr=z-1;
                    }
                }
}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>v[i];
    fin>>m;
    for(i=1;i<=m;i++)
    {
        ok=0;
        fin>>cond>>x;
        st=1;
        dr=n;
        if(cond==0)
            {
                fct(x);
                if(ok==0)
                    fout<<-1;
                else
                    {
                        z--;
                        fout<<z<<'\n';
                    }
            }
        else
        {
            if(cond==1||cond==2);
            {
                if(cond==1)
                {
                    while(ok==0)
                    {
                        fct(x);
                        x--;
                    }
                    z--;
                    fout<<z<<'\n';
                }
                else
                {
                    while(ok==0)
                    {
                        fct2(x);
                        x++;
                    }
                    z++;
                    fout<<z<<'\n';
                }
        }
    }
    }
    return 0;
}