Artifact Content
Not logged in

Artifact 5fcb34f3f46b9954eae3dbd6883dd337f3957590


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class FractalPicture { public:
	double getLength(int x1, int y1, int x2, int y2) 
	{
		return rec(1, x1, y1, x2, y2);
	}

	double rec(int i, int x1, int y1, int x2, int y2)
	{
		x1 = clip<-27,+27>(x1);
		x2 = clip<-27,+27>(x2);
		y1 = clip<  0,+81>(y1);
		y2 = clip<  0,+81>(y2);

		if( i == 501 )
			return 0;
		if( x1<=-27 && 27<=x2 && y1<=0 && 81<=y2 )
			return 81 + (500-i)*54;
		if( x2<=-27 || 27<=x1 || y2<=0 || 81<=y1 )
			return 0;
		double L0 = (x1<=0 && 0<=x2 ? clip<0,54>(y2)-clip<0,54>(y1) : 0);
		x1=x1*3, x2=x2*3, y1=(y1-54)*3, y2=(y2-54)*3;
		double L1 = rec(i+1, x1, y1, x2, y2);
		double L2 = rec(i+1, y1, x1, y2, x2);
		double L3 = rec(i+1, y1, -x2, y2, -x1);
		return L0 + (L1+L2+L3)/3;
	}

	template<int m, int M>
	int clip(int v) { return max(m, min(v, M)); }
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, FractalPicture().getLength(x1, y1, x2, y2));}
int main(){

CASE(0)
	int x1 = -1; 
	int y1 = 0; 
	int x2 = 1; 
	int y2 = 53; 
	double _ = 53.0; 
END
CASE(1)
	int x1 = 1; 
	int y1 = 1; 
	int x2 = 10; 
	int y2 = 10; 
	double _ = 0.0; 
END
CASE(2)
	int x1 = -10; 
	int y1 = 54; 
	int x2 = 10; 
	int y2 = 55; 
	double _ = 21.0; 
END
CASE(3)
	int x1 = 14; 
	int y1 = 45; 
	int x2 = 22; 
	int y2 = 54; 
	double _ = 2999.0; 
END
CASE(4)
	int x1 = -28;
	int y1 = 44; 
	int x2 = -16; 
	int y2 = 62; 
	double _ = 6992.0; 
END
CASE(5)
	int x1 = 0;
	int y1 = 80; 
	int x2 = 1; 
	int y2 = 81; 
	double _ = 332; 
END
}
// END CUT HERE