Cod sursa(job #3204592)
Utilizator | Data | 17 februarie 2024 09:36:07 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.53 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,i,c,x,v[100005],st,dr,rez,mij;
int main()
{
fin>>n;
for(i=1; i<=n; i++)
fin>>v[i];
sort(v+1,v+n+1);
fin>>m;
for(i=1; i<=m; i++)
{
fin>>c>>x;
st=1;
dr=n;
rez=-1;
if(c==0)
{
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
rez=mij;
st=mij+1;
}
else if(v[mij]<x)
st=mij+1;
else if(v[mij]>x)
dr=mij-1;
}
fout<<rez<<'\n';
}
else if(c==1)
{
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
{
rez=mij;
st=mij+1;
}
else if(v[mij]>x)
dr=mij-1;
}
fout<<rez<<'\n';
}
else
{
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]<x)
st=mij+1;
else if(v[mij]>=x)
{
dr=mij-1;
rez=mij;
}
}
if(v[dr]==x)
rez=dr;
fout<<rez<<'\n';
}
}
return 0;
}