Pagini recente » Cod sursa (job #1549774) | Cod sursa (job #1437538) | Cod sursa (job #332794) | Cod sursa (job #2801254) | Cod sursa (job #2974067)
#include<bits/stdc++.h>
using namespace std;
const int N = 100009;
unsigned n, v[N];
ifstream in ("cautbin.in");
ofstream out("cautbin.out");
// auto& in = cin;
// auto& out = cout;
unsigned upper_bound(int x)
{
unsigned poz=0;
for(unsigned i=1<<31; i; i>>=1)
if((poz+i) < n && v[poz+i] <= x)
{
// out<<"i: "<<i<<endl;
// out<<"poz|i: "<<(poz|i)<<endl;
poz+=i;
}
return poz;
}
unsigned lower_bound(int x)
{
unsigned poz=0;
for(unsigned i=1<<31; i; i>>=1)
if((poz+i) < n && v[poz+i] < x)
{
// out<<"i: "<<i<<endl;
// out<<"poz+i: "<<(poz+i)<<endl;
poz+=i;
}
if(v[poz] != x) poz++;
return poz;
}
int main(){
in>>n;
for(unsigned i=0; i < n; i++)
in>>v[i];
int m, t, x, u, l;
for(in>>m; m>0; m--)
{
in>>t>>x;
// out<<"x:"<<x<<endl;
u = upper_bound(x) + 1;
l = lower_bound(x) + 1;
if(t==0) out<<(l<u?u:-1)<<endl;
if(t==1) out<<u<<endl;
if(t==2) out<<l<<endl;
}
return 0;
}