Cod sursa(job #2021952)

Utilizator mihai.alphamihai craciun mihai.alpha Data 15 septembrie 2017 00:15:05
Problema Fibo3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <cstdio>
#include <vector>
#include <algorithm>

FILE *fin, *fout;

#define MAXNR 2e15 + 1
#define ll long long

int N;

std::vector <ll> fibi;

ll X1, Y1, X2, Y2;
bool ok = 1;

inline void calc(ll x, ll x1, ll y1, ll x2, ll y2) {
	ok = 1;
	X1 = x - y1;
	Y1 = x - x1;
	if (!(X1 >= x1 && X1 <= x2)) {
		X1 = x1;
	}
	else
		Y1 = y1;
	X2 = x - y2;
	Y2 = x - x2;
	if (!(X2 >= x1 && X2 <= x2)) {
		X2 = x2;
	}
	else
		Y2 = y2;
}

int main() {
	fin = fopen("fibo3.in", "r");
	fout = fopen("fibo3.out", "w");
	fscanf(fin, "%d", &N);
	fibi.push_back(1);
	fibi.push_back(1);
	while (fibi.back() <= MAXNR)
		fibi.push_back(fibi.back() + fibi[fibi.size() - 2]);
	fibi.pop_back();
	for (int i = 1; i <= N; i++) {
		ll x1, y1, x2, y2;
		fscanf(fin, "%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
		if (x1 > x2)
			std::swap(x1, x2);
		if (y1 < y2) {
			std::swap(y1, y2);
		}
		ll low, high;
		low = std::min(x1, x2) + std::min(y1, y2);
		high = std::max(x1, x2) + std::max(y1, y2);
		ll ans = 0LL;
		bool Ok = 1;
	//	printf("%lld %lld %lld %lld\n", x1, y1, x2, y2);
		for (unsigned int j = 1; j < fibi.size() && Ok;j++) {
			ll x = fibi[j];
			if (x >= low && x <= high) {
				calc(x, x1, y1, x2, y2);
				if (ok == 1) {
					ll dif = X2 - X1 + 1;
					//			if (i == 2) {
						//			printf("%lld %lld %lld %lld\n", X1, Y1, X2, Y2);
						//		}
					ans += dif;
				}
			}
			else if (x > high)
				Ok = 0;
		}
		fprintf(fout, "%lld\n", ans);
	}
	fclose(fin);
	fclose(fout);
	return 0;
}