96fdfab500 2011-03-20 kinaba: #include <iostream> 96fdfab500 2011-03-20 kinaba: #include <sstream> 96fdfab500 2011-03-20 kinaba: #include <iomanip> 96fdfab500 2011-03-20 kinaba: #include <vector> 96fdfab500 2011-03-20 kinaba: #include <string> 96fdfab500 2011-03-20 kinaba: #include <map> 96fdfab500 2011-03-20 kinaba: #include <set> 96fdfab500 2011-03-20 kinaba: #include <algorithm> 96fdfab500 2011-03-20 kinaba: #include <numeric> 96fdfab500 2011-03-20 kinaba: #include <iterator> 96fdfab500 2011-03-20 kinaba: #include <functional> 96fdfab500 2011-03-20 kinaba: #include <complex> 96fdfab500 2011-03-20 kinaba: #include <queue> 96fdfab500 2011-03-20 kinaba: #include <stack> 96fdfab500 2011-03-20 kinaba: #include <cmath> 96fdfab500 2011-03-20 kinaba: #include <cassert> 96fdfab500 2011-03-20 kinaba: #include <cstring> 96fdfab500 2011-03-20 kinaba: using namespace std; 96fdfab500 2011-03-20 kinaba: typedef long long LL; 96fdfab500 2011-03-20 kinaba: typedef complex<double> CMP; 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: class FractalPicture { public: 96fdfab500 2011-03-20 kinaba: double getLength(int x1, int y1, int x2, int y2) 96fdfab500 2011-03-20 kinaba: { 96fdfab500 2011-03-20 kinaba: return rec(1, x1, y1, x2, y2); 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: double rec(int i, int x1, int y1, int x2, int y2) 96fdfab500 2011-03-20 kinaba: { 96fdfab500 2011-03-20 kinaba: x1 = clip<-27,+27>(x1); 96fdfab500 2011-03-20 kinaba: x2 = clip<-27,+27>(x2); 96fdfab500 2011-03-20 kinaba: y1 = clip< 0,+81>(y1); 96fdfab500 2011-03-20 kinaba: y2 = clip< 0,+81>(y2); 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: if( i == 501 ) 96fdfab500 2011-03-20 kinaba: return 0; 96fdfab500 2011-03-20 kinaba: if( x1<=-27 && 27<=x2 && y1<=0 && 81<=y2 ) 96fdfab500 2011-03-20 kinaba: return 81 + (500-i)*54; 96fdfab500 2011-03-20 kinaba: if( x2<=-27 || 27<=x1 || y2<=0 || 81<=y1 ) 96fdfab500 2011-03-20 kinaba: return 0; 96fdfab500 2011-03-20 kinaba: double L0 = (x1<=0 && 0<=x2 ? clip<0,54>(y2)-clip<0,54>(y1) : 0); 96fdfab500 2011-03-20 kinaba: x1=x1*3, x2=x2*3, y1=(y1-54)*3, y2=(y2-54)*3; 96fdfab500 2011-03-20 kinaba: double L1 = rec(i+1, x1, y1, x2, y2); 96fdfab500 2011-03-20 kinaba: double L2 = rec(i+1, y1, x1, y2, x2); 96fdfab500 2011-03-20 kinaba: double L3 = rec(i+1, y1, -x2, y2, -x1); 96fdfab500 2011-03-20 kinaba: return L0 + (L1+L2+L3)/3; 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: template<int m, int M> 96fdfab500 2011-03-20 kinaba: int clip(int v) { return max(m, min(v, M)); } 96fdfab500 2011-03-20 kinaba: }; 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: // BEGIN CUT HERE 96fdfab500 2011-03-20 kinaba: #include <ctime> 96fdfab500 2011-03-20 kinaba: double start_time; string timer() 96fdfab500 2011-03-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 96fdfab500 2011-03-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 96fdfab500 2011-03-20 kinaba: { os << "{ "; 96fdfab500 2011-03-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 96fdfab500 2011-03-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 96fdfab500 2011-03-20 kinaba: void verify_case(const double& Expected, const double& Received) { 96fdfab500 2011-03-20 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 96fdfab500 2011-03-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 96fdfab500 2011-03-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 96fdfab500 2011-03-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 96fdfab500 2011-03-20 kinaba: #define END verify_case(_, FractalPicture().getLength(x1, y1, x2, y2));} 96fdfab500 2011-03-20 kinaba: int main(){ 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: CASE(0) 96fdfab500 2011-03-20 kinaba: int x1 = -1; 96fdfab500 2011-03-20 kinaba: int y1 = 0; 96fdfab500 2011-03-20 kinaba: int x2 = 1; 96fdfab500 2011-03-20 kinaba: int y2 = 53; 96fdfab500 2011-03-20 kinaba: double _ = 53.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(1) 96fdfab500 2011-03-20 kinaba: int x1 = 1; 96fdfab500 2011-03-20 kinaba: int y1 = 1; 96fdfab500 2011-03-20 kinaba: int x2 = 10; 96fdfab500 2011-03-20 kinaba: int y2 = 10; 96fdfab500 2011-03-20 kinaba: double _ = 0.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(2) 96fdfab500 2011-03-20 kinaba: int x1 = -10; 96fdfab500 2011-03-20 kinaba: int y1 = 54; 96fdfab500 2011-03-20 kinaba: int x2 = 10; 96fdfab500 2011-03-20 kinaba: int y2 = 55; 96fdfab500 2011-03-20 kinaba: double _ = 21.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(3) 96fdfab500 2011-03-20 kinaba: int x1 = 14; 96fdfab500 2011-03-20 kinaba: int y1 = 45; 96fdfab500 2011-03-20 kinaba: int x2 = 22; 96fdfab500 2011-03-20 kinaba: int y2 = 54; 96fdfab500 2011-03-20 kinaba: double _ = 2999.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(4) 96fdfab500 2011-03-20 kinaba: int x1 = -28; 96fdfab500 2011-03-20 kinaba: int y1 = 44; 96fdfab500 2011-03-20 kinaba: int x2 = -16; 96fdfab500 2011-03-20 kinaba: int y2 = 62; 96fdfab500 2011-03-20 kinaba: double _ = 6992.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(5) 96fdfab500 2011-03-20 kinaba: int x1 = 0; 96fdfab500 2011-03-20 kinaba: int y1 = 80; 96fdfab500 2011-03-20 kinaba: int x2 = 1; 96fdfab500 2011-03-20 kinaba: int y2 = 81; 96fdfab500 2011-03-20 kinaba: double _ = 332; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: // END CUT HERE