Pagini recente » Cod sursa (job #2654662) | Cod sursa (job #1456411) | Cod sursa (job #401669) | Cod sursa (job #2507018) | Cod sursa (job #2569344)
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
long long n,a[100001],i,mij,st,dr,m,x,t,poz;
int main()
{
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
f>>m;
for(i=1;i<=m;i++)
{
f>>t>>x;
st=1;
dr=n;
if(t==0)
{
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]<x)
st=mij+1;
else
if(a[mij]>x)
dr=mij-1;
else
if(a[mij]==x)
{
st=mij+1;
if(mij>poz)
poz=mij;
}
}
if(poz==0)
g<<-1<<endl;
else
g<<poz<<endl;
}
if(t==1)
{
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]<=x)
{
st=mij+1;
if(mij>poz)
poz=mij;
}
else
if(a[mij]>x)
dr=mij-1;
}
g<<poz<<endl;
}
if(t==2)
{
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]<x)
st=mij+1;
if(a[mij]>=x)
{
dr=mij-1;
if(mij<poz)
poz=mij;
}
}
g<<poz<<endl;
}
}
}