Cod sursa(job #1837704)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 30 decembrie 2016 12:31:25
Problema Fibo3 Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <bits/stdc++.h>
#define SIZE 100
#define MAXSUM 2000000000000000LL
long long fib[SIZE+1];
inline long long getmax(long long a,long long b){
    if(a<b) return b;
    return a;
}
inline long long getmin(long long a,long long b){
    if(a<b) return a;
    return b;
}
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,xmax,xmin,ymax,ymin;
    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;
       xmin=getmin(x1,x2);
       xmax=getmax(x1,x2);
       ymin=getmin(y1,y2);
       ymax=getmax(y1,y2);
       x1=xmin;
       x2=xmax;
       y1=ymin;
       y2=ymax;
       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;
}