Cod sursa(job #2776404)
Utilizator | Data | 19 septembrie 2021 16:51:55 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.54 kb |
#include<bits/stdc++.h>
using namespace std;
ifstream F("cautbin.in");
ofstream G("cautbin.out");
int a[100100],m,n,x,y,i;
int main()
{
F>>n;
for(i=0;i<n;++i)
F>>a[i];
F>>m;
while(m--) {
F>>y>>x;
if(!y) {
y=upper_bound(a,a+n,x)-a;
if(a[y-1]==x&&y)
G<<y<<"\n";
else
G<<"-1\n";
} else if(y==1)
G<<upper_bound(a,a+n,x)-a<<"\n";
else
G<<lower_bound(a,a+n,x)-a+1<<"\n";
}
return 0;
}