Pagini recente » Istoria paginii runda/jc2023-runda1 | Cod sursa (job #1697246) | Cod sursa (job #661628) | Cod sursa (job #1347857) | Cod sursa (job #661609)
Cod sursa(job #661609)
#include<fstream>
#include<iostream>
using namespace std;
int main()
{ int v[100002], N, M, caz, x, mid, poz, st, dr;
fstream f("cautbin.in", ios::in);
fstream g("cautbin.out", ios::out);
f>>N;
for(int i=1;i<=N;i++)
f>>v[i];
f>>M;
for(int i=1;i<=M;i++){
f>>caz>>x; dr=N; st=1;
if(caz==0)
{ while(st<=dr)
{mid=st+(dr-st)/2;
if(v[mid]>x)
dr=mid-1;
else
st=mid+1; }
poz=(st+dr)/2;
if(v[poz]>x) poz--;
if(v[poz]==x)g<<poz<<endl;
else g<<"-1"<<endl;}
if(caz==1)
{ while(st<dr)
{mid=st+(dr-st)/2;
if(v[mid]>x)
dr=mid;
else st=mid+1; }
poz=(st+dr)/2;
if(v[poz]>x) poz--;
g<<poz<<endl; }
if(caz==2)
{ while(st<dr)
{mid=st+(dr-st)/2;
if(v[mid]>=x)
dr=mid;
else st=mid+1; }
poz=(st+dr)/2;
if(v[poz]<x) poz++;
g<<poz<<endl;
}}
f.close();
g.close();
return 0;
}