Cod sursa(job #2357557)

Utilizator robertbirsanRobert Birsan robertbirsan Data 27 februarie 2019 15:42:32
Problema Numarare triunghiuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");

long n,x[100010],i,m,a,b,poz;

int cb1(int val)
{
    int st,dr,p,m;
    dr=1;
    st=1;
    p=n;
    while(st<=p)
    {
        m=(st+p)/2;
        if(val>=x[m])
        {
            dr=m;
            st=m+1;
        }
        else
            p=m-1;
    }
    return dr;
}

int cb3(int val)
{
    int st,p,dr=-1,m;
    st=1;
    p=n;
    while(st<=p)
    {
        m=(st+p)/2;
        if(val<=x[m])
        {
            dr=m;
            p=m-1;
        }
        else
            st=m+1;
    }
    return dr;
}

void Solve()
{
    f>>n;
    for(i=1;i<=n;i++)
        f>>x[i];
    f>>m;
    for(i=1;i<=m;i++)
    {
        f>>a>>b;
        poz=cb1(b);
        if(a==0)
            if(x[poz]==b)
            g<<poz<<'\n';
        else
            g<<-1<<'\n';
        else
            if(a==1)
            g<<poz<<'\n';
        else
            g<<cb3(b)<<'\n';
    }
}
int main()
{
    Solve();
    return 0;
}