Pagini recente » Cod sursa (job #1807501) | Cod sursa (job #2639663) | Cod sursa (job #3259222) | Cod sursa (job #3155388) | Cod sursa (job #870352)
Cod sursa(job #870352)
#include <stdio.h>
#include <algorithm>
using namespace std;
FILE *in,*out;
int num, operations;
int answer;
int type, value;
int op[100100];
int main ()
{
in=fopen("cautbin.in", "rt");
out=fopen("cautbin.out", "wt");
fscanf(in, "%d", &num);
for (int i = 1; i <= num; ++ i)
fscanf(in,"%d", &op[i]);
fscanf(in,"%d", &operations);
while(operations--)
{
fscanf(in,"%d%d", &type, &value);
if (!type)
{
answer = upper_bound(op + 1, op + num + 1, value) - op - 1;
if (1 <= answer && answer <= num && op[answer] == value)
fprintf(out,"%d\n", answer);
else
fprintf(out,"-1\n");
}
else if (type == 1)
fprintf(out,"%d\n", lower_bound(op + 1, op + num + 1, value + 1) - op - 1);
else fprintf(out,"%d\n", upper_bound(op + 1, op + num + 1, value - 1) - op);
}
fclose(in);
fclose(out);
return 0;
}