Pagini recente » Cod sursa (job #1839438) | Cod sursa (job #833083) | Cod sursa (job #3271598) | Cod sursa (job #2171751) | Cod sursa (job #2415728)
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g ("cautbin.out");
#define ll long long
ll n,m,i,j,x,a,b;
vector<ll>v;
ll bin(vector<ll>v,ll l,ll h, ll y)
{
if(l>h) return -1;
ll mid = (l+h)/2;
if(v[mid]==y) return mid;
if(v[mid]>y) return bin(v,l,mid-1,y);
return bin(v,mid+1,h,y);
}
int main()
{
f>>n;
for(i=1;i<=n;i++)
{
f>>x;
v.push_back(x);
}
f>>m;
for(i=1;i<=m;i++)
{
f>>a>>b;
ll rez = bin(v,0,n-1,b);
if(a==0){if (rez==-1) g<<rez<<endl;
else while(v[rez]==b) rez++;g<<rez<<endl;
}
else if(a==1){
if(rez==-1)
{
b--;
while(bin(v,0,n-1,b)==-1) b--;
b++;
rez=bin(v,0,n-1,b);
while(v[rez]==b) rez++;
g<<rez<<endl;
}
else if (rez!=-1)
{
while(v[rez]==b) rez++;g<<rez<<endl;
}
}
else if(a==2)
{
if (rez==-1)
{
b++;
while(bin(v,0,n-1,b)==-1) b++;
b--;
rez=bin(v,0,n-1,b);
while (v[rez]==b) rez--;rez++;
g<<rez+1<<endl;
}
else if(rez!=-1)
{
while(v[rez]==b) rez--;g<<rez+2<<endl;
}
}
}
return 0;
}