Cod sursa(job #1962184)

Utilizator linerunnerMihai Ion linerunner Data 11 aprilie 2017 16:57:26
Problema Cautare binara Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

#define DIMMAX 100001

int vect[DIMMAX];
int q0[DIMMAX];
int q1[DIMMAX];
int q2[DIMMAX];

int main()
{

	int no_elem, no_query;
	int i, q_type, val, poz;
	std::vector<int>::iterator low, up;

	FILE *f = fopen("cautbin.in", "r");
	fscanf(f, "%d", &no_elem);

	for (i = 1; i <= no_elem; i++)
		fscanf(f, "%d", &vect[i]);

	std::vector<int> v(vect + 1, vect + no_elem + 1);

	fscanf(f, "%d", &no_query);

	FILE *fout = fopen("cautbin.out", "w");

	for (i = 0; i < no_query; i++) {
		fscanf(f, "%d", &q_type);
		fscanf(f, "%d", &val);

		if (q_type == 0) {
			if (q0[val] == 0) {
				poz = std::upper_bound(v.begin(), v.end(), val) - v.begin();
				if ( 1 <= poz && poz <= no_elem && vect[poz] == val ) {
					fprintf(fout, "%d\n", poz);
					q0[val] = poz;
				}
				else {
					fprintf(fout, "-1\n");
				}
			}
			else
				fprintf(fout, "%d\n", q0[val]);
		}

		if (q_type == 1) {
			if (q1[val] == 0) {
				poz = std::lower_bound(v.begin(), v.end(), val + 1) - v.begin();
				fprintf(fout, "%d\n", poz);
				q1[val] = poz;
			}
			else
				fprintf(fout, "%d\n", q1[val]);
		}

		if (q_type == 2) {
			if (q2[val] == 0) {
				poz = std::upper_bound(v.begin(), v.end(), val - 1) - v.begin() + 1;
				fprintf(fout, "%d\n", poz);
				q2[val] = poz;
			}
			else
				fprintf(fout, "%d\n", q2[val]);
		}
	}

	fclose(fout);
	fclose(f);

	return 0;
}