Pagini recente » Cod sursa (job #1266295) | Cod sursa (job #126901) | Cod sursa (job #2029023) | Cod sursa (job #3198113) | Cod sursa (job #1610916)
#include <iostream>
#include <cstdio>
using namespace std;
FILE *f,*g;
const int N = 100000, L = 16;
int n,m,i,v[100001],caz,x,nr;
int caut0(int x)
{
int i, pas;
i = 0;
pas = 1 << L;
while (pas != 0)
{
if (i + pas <= n)
if(v[i + pas] <= x)
i += pas;
pas /= 2; //pas >>= 1
}
if (v[i] != x)
return -1;
return i;
}
int caut1(int x)
{
int i, pas;
i = 0;
pas = 1 << L;
while (pas != 0)
{
if (i + pas <= n )
if(v[i + pas] <= x)
i += pas;
pas /= 2; //pas >>= 1
}
return i;
}
int caut2(int x)
{
int i, pas;
i = 0;
pas = 1 << L;
while (pas != 0)
{
if (i + pas <= n )
if(v[i + pas] < x)
i += pas;
pas /= 2; //pas >>= 1
}
return 1 + i;
}
int main()
{
f=fopen("cautbin.in","r");
g=fopen("cautbin.out","w");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
{
fscanf(f,"%d",&v[i]);
}
fscanf(f,"%d",&m);
for(i=1;i<=m;i++)
{
fscanf(f,"%d%d",&caz,&x);
if(caz==0)
{
nr=caut0(x);
fprintf(g,"%d\n",nr);
}
if(caz==1)
{
nr=caut1(x);
fprintf(g,"%d\n",nr);
}
if(caz==2)
{
nr=caut2(x);
fprintf(g,"%d\n",nr);
}
}
fclose(f);
fclose(g);
return 0;
}