Cod sursa(job #3366395)
| Utilizator | Data | 1 octombrie 2026 12:12:38 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 80 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.67 kb |
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("cautbin.in");
ofstream cout("cautbin.out");
long long v[100100];
int main()
{
long long n,m,x,c,q;
cin>>n;
for(int i=0;i<n;i++){
cin>>v[i];
}
cin>>m;
while(m--){
cin>>c>>q;
if(c==0){
x=upper_bound(v,v+n,q)-v;
if(x<n&&x>=0&&v[x-1]==q) cout<<x<<'\n';
else cout<<-1<<'\n';
}
else if(c==1){
x=lower_bound(v,v+n,q+1)-v;
cout<<x<<'\n';
}
else{
x=upper_bound(v,v+n,q-1)-v+1;
cout<<x<<'\n';
}
}
}
