Cod sursa(job #2989558)

Utilizator Redstoneboss2Fabian Lucian Redstoneboss2 Data 6 martie 2023 19:19:25
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int main(){

    int arraySize, queryCount;
    vector<long long> elements;

    fin >> arraySize;
    elements.resize(arraySize);

    for(int i = 0; i < arraySize; i++)
        fin >> elements[i];
    
    fin >> queryCount;

    for(long long i = 0, queryType, queryValue; i < queryCount; i++){
        fin >> queryType >> queryValue;

        vector<long long>::iterator indexFound;

        if(queryType == 0){
            indexFound = upper_bound(elements.begin(), elements.end(), queryValue);

            if(*(indexFound - 1) == queryValue)
                fout << indexFound - elements.begin() << '\n';
            else
                fout << "-1\n";
        }else if(queryType == 1){
            indexFound = upper_bound(elements.begin(), elements.end(), queryValue);

            fout << indexFound - elements.begin() << '\n';
        }else{
            indexFound = lower_bound(elements.begin(), elements.end(), queryValue);

            fout << indexFound - elements.begin() + 1 << '\n';
        }
    }

    return 0;
}