Cod sursa(job #464600)

Utilizator savimSerban Andrei Stan savim Data 20 iunie 2010 23:39:51
Problema Fibo3 Scor Ascuns
Compilator cpp Status done
Runda Marime 1.69 kb
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

long long fib[100];
long long x1, y1, x2, y2;

int n;

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);

		long long startPozition = x1 + y1;
		long long stopPozition = x2 + y1;

		int pozFirst = 0, pozLast = 0;
		long long firstElement = 0, lastElement = 0;

		for (int j = 1; j < 100; j++)
			if (fib[j] >= startPozition) {
				firstElement = fib[j]; 
				pozFirst = j;
				break;
			}

		for (int j = 1; j < 100; j++)
			if (fib[j] > startPozition + (y2 - y1)) {
            	lastElement = fib[j];
				pozLast = j;
				break;
			}
                
		long long retValue = 0, Sum = pozLast - pozFirst;
		while (startPozition <= stopPozition) {
			long long firstEvent = firstElement - startPozition + 1;
			long long secondEvent = lastElement - startPozition - (y2 - y1);
			long long thirdEvent = stopPozition - startPozition + 1;

			if (thirdEvent < min(firstEvent, secondEvent)) {
            	retValue += 1LL * thirdEvent * Sum;
				startPozition += thirdEvent;
			}
			else {
            	long long EvMin = min(firstEvent, secondEvent);

				retValue += EvMin * Sum;
	
				if (firstEvent == EvMin) {
                	pozFirst++;
					firstElement = fib[pozFirst];
					Sum--;
				}

				if (secondEvent == EvMin) {
                	pozLast++;
				 	lastElement = fib[pozLast];
					Sum++;
				}
				
				startPozition += EvMin;
			}
		}

		printf("%lld\n", retValue);
	}
}

int main() {
	fib[0] = fib[1] = 1;
	for (int i = 2; i < 100; i++)
		fib[i] = fib[i - 2] + fib[i - 1];

	freopen("fib3.in", "r", stdin);
	freopen("fib3.out", "w", stdout);

	solve();

	return 0;
}