Cod sursa(job #2370883)
| Utilizator | Data | 6 martie 2019 14:17:05 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.79 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, m, x, y, sol, a[100005];
int main()
{
fin >> n;
for(int i=1; i<=n; i++)
fin >> a[i];
fin >> m;
while(m--)
{
fin >> x >> y;
switch(x)
{
case 0:
sol=upper_bound(a+1, a+n+1, y)-a-1;
if(a[sol]==y)
fout << sol << '\n';
else
fout << "-1\n";
break;
case 1:
fout << lower_bound(a+1, a+n+1, y+1)-a-1 << '\n';
break;
case 2:
fout << upper_bound(a+1, a+n+1, y-1)-a << '\n';
break;
}
}
return 0;
}
