Cod sursa(job #1837689)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 30 decembrie 2016 12:20:23
Problema Fibo3 Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>
#define SIZE 100
#define MAXSUM 2000000000000000LL
long long fib[SIZE+1];
int main(){
    std::ifstream fin("fibo3.in");
    std::ofstream fout("fibo3.out");
    int t,nf,rez,pas,i;
    long long x1,x2,y1,y2,s1,s2,ans;
    fin >> t;
    fib[0]=1;
    fib[1]=1;
    nf=2;
    while(fib[nf-1]+fib[nf-2]<=MAXSUM){
       fib[nf]=fib[nf-1]+fib[nf-2];
       nf++;
    }
    nf--;
    while(t>0){
       t--;
       fin >> x1 >> y1 >> x2 >> y2;
       s1=x1+y1;
       s2=x2+y2;
       rez=0;
       for(pas=1<<6;pas;pas>>=1)
         if(rez+pas<=nf&&fib[rez+pas]<s1)
           rez+=pas;
       i=rez+1;
       ans=0;
       while(fib[i]<=s2){
          if(y2-y1<=x2-x1){
             if(fib[i]-s1<=y2-y1)
                ans+=fib[i]-s1+1;
             else if(s2-fib[i]<=y2-y1)
                ans+=s2-fib[i]+1;
             else
                ans+=y2-y1+1;
          }
          else{
             if(fib[i]-s1<=x2-x1)
                ans+=fib[i]-s1+1;
             else if(s2-fib[i]<=x2-x1)
                ans+=s2-fib[i]+1;
             else
                ans+=x2-x1+1;
          }
          i++;
       }
       fout << ans << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}