Cod sursa(job #2028945)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 28 septembrie 2017 21:08:37
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
# pragma GCC optimize("O3")
# include <bits/stdc++.h>
# define maxn 100005
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define sz(x) (int)((x).size())
//# define int ll
using namespace std;

int n,x,op,q;
vector<int>vec;

int32_t main(){_
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> x;
        vec.pb(x);
    }    
    cin >> q;
    while(q--)
    {
        cin >> op >> x;
        if(op == 0)
        {
            auto it = upper_bound(vec.begin(),vec.end(),x);
            it--;
            if(*it == x) cout << it - vec.begin() + 1 << '\n';
            else cout << "-1\n";
        }
        else if(op == 1)
        {
            auto it = upper_bound(vec.begin(),vec.end(),x);
            it--;
            cout << it - vec.begin() + 1 << '\n';
        }
        else if(op == 2)
        {
            auto it = lower_bound(vec.begin(),vec.end(),x);
            cout << it - vec.begin() + 1 << '\n';
        }
    }
}