Cod sursa(job #518001)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 30 decembrie 2010 13:06:55
Problema Fibo3 Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
# include <fstream.h>
# define  LL  long long
# define  MAX  1000000000000000LL
  	ifstream f ("fibo3.in");
	ofstream g ("fibo3.out");
	LL n, x1, x2, Y1, y2;
	LL ferb[1000];
	LL i, siz;
	int cb (LL in, LL sf, LL find){
		int ret;
		while (in <= sf){
			LL m = (LL)((in + sf) >> 1);
			if (ferb[m] >= find){
				ret = m;
				sf = m - 1;
			}
			else in = m + 1;
		}
		return ret;
	}
	inline int minim (LL a, LL b){
		return (a > b ? b : a);
	}
	int main (){
		ferb[0] = 1;
		ferb[1] = 1;
		for (i = 1; ferb[i - 1] <= MAX; ++i, ferb[i] = ferb[i - 1] + ferb[i - 2]);
		siz = i;
		for (f >> n; n > 0; --n){
			f >> x1 >> Y1 >> x2 >> y2;
			i = cb (1, siz, x1 + Y1);
			LL SUM = x2 + y2, sol = 0;
			if (!i) i = 1;
			while (ferb[i] <= SUM){
				if (ferb[i] - x1 <= y2){//il ai pe linie
					sol += minim (ferb[i] - Y1 - x1 + 1, x2 - x1 + 1);
				}
				else{//il ai pe coloana
					sol += minim (x2 - ferb[i] + y2 + 1, y2 - Y1 + 1);
				}
				++i;
			}
			g << sol << '\n';
		} 
		g.close ();
		return 0;
	}