Skip to content

Instantly share code, notes, and snippets.

@satellitex
Created July 24, 2014 13:45
Show Gist options
  • Save satellitex/322b2be7296eb9186fa1 to your computer and use it in GitHub Desktop.
Save satellitex/322b2be7296eb9186fa1 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define X first
#define Y second
int T;
int fw[1000005];
int fh[1000005];
vector<P> W;
vector<P> H;
inline void umeW(P *w){
if( !fw[w->X] && !fh[w->Y] ){
W.push_back( P(w->X,w->Y) );
fw[w->X] = 1;
fh[w->Y] = 1;
w->Y--;
}
w->X++; w->Y++;
}
inline void umeH(P *h){
if( !fw[h->X] && !fh[h->Y] ){
H.push_back( P(h->X,h->Y) );
fh[h->Y] = 1;
fw[h->X] = 1;
h->X--;
}
h->X++; h->Y++;
}
inline void init(){
P w = P(2,1);
P h = P(1,2);
while( 1 ){
if( (w.X > 1000000 || w.Y > 1000000) &&
(h.X > 1000000 || h.Y > 1000000) ) break;
umeH(&h); umeW(&w);
}
}
inline int check(int x1,int y1,int x2,int y2,P a){
if( x1 <= a.X && a.X <= x2 && y1 <= a.Y && a.Y <= y2 ) return 1;//in
if( a.X > x2 || a.Y > y2 ) return 2;//upper
return 0;//lower
}
inline int upper(int x1,int y1,int x2,int y2,const vector<P> &R){
int st = 0, ed = R.size()-1;
int res=-2;
while( st <= ed ){
int h = (st+ed)/2;
int ret = check(x1,y1,x2,y2,R[h]);
if( ret <=1 ){
if( ret == 1 ) res = max(res,h);
st = h+1;
} else {
ed = h-1;
}
}
return res;
}
inline int lower(int x1,int y1,int x2,int y2,const vector<P> &R){
int st = 0, ed = R.size()-1;
int res=R.size();
while( st <= ed ){
int h = (st+ed)/2;
int ret = check(x1,y1,x2,y2,R[h]);
if( ret < 1 ){
st = h+1;
} else {
if( ret==1 ) res = min(res,h);
ed = h-1;
}
}
if( res == R.size()) res = -1;
return res;
}
inline int result(int x1,int y1,int x2,int y2){
return upper(x1,y1,x2,y2,W) - lower(x1,y1,x2,y2,W) + upper(x1,y1,x2,y2,H) - lower(x1,y1,x2,y2,H) +2;
}
typedef long long ll;
int main(){
init();
cin >> T;
for(int t=1;t<=T;t++){
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
ll all = (ll)(x2-x1+1)*(ll)(y2-y1+1);
if( x1 == 0 && y1 == 0 ) all--;
ll ret = (ll)result(x1,y1,x2,y2);
ret = all - ret;
if( all == 0 || ret == 0 ){
cout << "Board "<<t<<": " << 0 << " / " <<1<< endl;
} else {
ll r = __gcd(all,ret);
cout << "Board "<<t<<": " << ret/r << " / " <<all/r<< endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment