Pagini recente » Cod sursa (job #1149105) | Cod sursa (job #2524114) | Cod sursa (job #832113) | Cod sursa (job #1766110) | Cod sursa (job #1035821)
# include <iostream>
# include <fstream>
# define nmax 100000
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,v[nmax],caz;
void caut(int st, int dr, int x)
{
int mij=(st+dr)/2,poz=mij;
if(v[mij]==x)
{
if(caz==0 || caz==1)
{
while(v[poz+1]==x)
poz+=1;
g<<poz<<'\n';
}
if(caz==2)
{
while(v[poz-1]==x)
poz-=1;
g<<poz<<'\n';
}
}
if(st==dr && caz==0)
g<<"-1";
if(dr-st==1)
{
if(caz==1)
g<<dr<<'\n';
if(caz==2)
g<<st<<'\n';
}
if(st<mij && v[mij]>x)
caut(st, mij-1, x);
if(dr>mij && v[mij]<x)
caut(mij+1, dr, x);
}
int main()
{
int i,m;
f>>n;
for(i=1; i<=n; i++)
f>>v[i];
f>>m;
int x;
for(i=1; i<=m; i++)
{
f>>caz>>x;
caut(1,n,x);
}
return 0;
}