Cod sursa(job #3157690)

Utilizator Alexbora13Bora Ioan Alexandru Alexbora13 Data 16 octombrie 2023 16:48:45
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
#define MAX 100000
using namespace std;

ifstream fin("cautbin.in");
ofstream fout("cautbin.out");

int v[MAX+1];
int m, n, c, x;

int main()
{
    fin >> n;
    for(int i=0; i<n; i++)fin >> v[i];
    fin >> m;
    for(int i=0; i<m; i++)
    {
        fin >> c >> x;
        if(c==0)
        {
            int pos = upper_bound(v,v+n,x)-v-1;
            if(v[pos]==x)fout << pos+1 << '\n';
            else fout << -1 << '\n';
        }
        else if(c==1)
        {
            int pos = lower_bound(v,v+n,x)-v;
            if(v[pos]<x)fout << pos+1 << '\n';
            if(v[pos]==x)
            {
                while(v[pos]==x)pos++;
                fout << pos << '\n';
            }
        }
        else if(c==2)
        {
            int pos=lower_bound(v,v+n,x)-v;
            fout << 1+pos << '\n';
        }
    }
    return 0;
}