Cod sursa(job #1756448)

Utilizator vlad2004SuntUnNanoTub vlad2004 Data 12 septembrie 2016 20:53:22
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.85 kb
#include <fstream>

using namespace std;
ifstream  in("cautbin.in");
ofstream out ("cautbin.out");
int v[10000000];
int main()
{
    int i,c,n,sol,m,x;
    in>>n;
    for(i=1; i<=n; i++)
    {
        in>>v[i];
    }
    in>>m;
    for(i=1; i<=m; i++)
    {
        in>>c>>x;
        if(c==0)
         {int n2 = 1;
                while ( n2*2 <= n ) {
                    n2 *= 2;
                }

                int sol = 0;
                    for ( int i = n2; i != 0; i /= 2 ) {
                        if ( sol+i <= n && v[sol+i] <= x ) {
                            sol += i;
                    }
                    }

                if ( v[sol] == x ) {
                    out<<v[sol]<<'\n';
                }
                else {
                    out << "-1\n";
                }
                for(i=1; i<=n; i++)
                {
                    if(v[i]==x and v[i]!=v[i+1])
                    out<<i<<'\n';
                }
          }
          if(c==1)
          {
                    int n2 = 1;
                        while ( n2*2 <= n ) {
                            n2 *= 2;
                        }

                    int sol = 0;
                        for ( int i = n2; i != 0; i /= 2 ) {
                            if ( sol+i <= n && v[sol+i] <= x ) {
                                sol += i;
                            }
                        }

                    if ( v[sol] == x ) {
                            out << v[sol] << '\n';
                    }
                    else {
                            for(i=1; i<=n; i++)
                            {
                                if(v[i+1]==x or v[i+1]>x)
                                out<<x<<'\n';

                            }
                         }
          }
          if(c==2)
          {
                        int n2 = 1;
                                while ( n2*2 <= n ) {
                                    n2 *= 2;
                                }

                        int sol = 0;
                                for ( int i = n2; i != 0; i /= 2 ) {
                                    if ( sol+i <= n && v[sol+i] <= x ) {
                                        sol += i;
                                    }
                                }

                                if ( v[sol] == x ) {
                                    out << v[sol] << '\n';
                                                   }
                                else {
                                    for(i=1; i<=n; i++)
                                    {
                                        if(v[i+1]==x or v[i+1]<x)
                                        out<<i<<'\n';
                                    }
                                }
          }
    }
    return 0;
}