4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> pt; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool byY( pt a, pt b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( a.imag() == b.imag() ) 4fd800b3a8 2011-02-23 kinaba: return a.real() < b.real(); 4fd800b3a8 2011-02-23 kinaba: return a.imag() < b.imag(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool byArg( pt a, pt b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return arg(a) < arg(b); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool isRight( pt a, pt b, pt c ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return arg((c-b) / (b-a)) < 0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<pt> convex_hull( vector<pt> p ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<pt>::iterator it = min_element( p.begin(), p.end(), byY ); 4fd800b3a8 2011-02-23 kinaba: pt o = *it; 4fd800b3a8 2011-02-23 kinaba: p.erase(it); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<p.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: p[i] -= o; 4fd800b3a8 2011-02-23 kinaba: sort( p.begin(), p.end(), byArg ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<pt> q; 4fd800b3a8 2011-02-23 kinaba: q.push_back( pt(0,0) ); 4fd800b3a8 2011-02-23 kinaba: q.push_back( p[0] ); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<p.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: while( isRight(q[q.size()-2], q[q.size()-1], p[i]) ) 4fd800b3a8 2011-02-23 kinaba: q.pop_back(); 4fd800b3a8 2011-02-23 kinaba: q.push_back( p[i] ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<q.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: q[i] += o; 4fd800b3a8 2011-02-23 kinaba: return q; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double outer_prod( pt a, pt b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return (a.real()*b.imag() - b.real()*a.imag())/2; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double area( const vector<pt>& q ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double a = 0.0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pt o = q[0]; 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i+1<q.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: a += outer_prod(q[i]-o, q[i+1]-o); 4fd800b3a8 2011-02-23 kinaba: return abs(a); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Shadow 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double area(vector <int> tree, vector <int> light) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double x1 = min(tree[0], tree[3]); 4fd800b3a8 2011-02-23 kinaba: double x2 = max(tree[0], tree[3]); 4fd800b3a8 2011-02-23 kinaba: double y1 = min(tree[1], tree[4]); 4fd800b3a8 2011-02-23 kinaba: double y2 = max(tree[1], tree[4]); 4fd800b3a8 2011-02-23 kinaba: double z1 = min(tree[2], tree[5]); 4fd800b3a8 2011-02-23 kinaba: double z2 = max(tree[2], tree[5]); 4fd800b3a8 2011-02-23 kinaba: double x = light[0]; 4fd800b3a8 2011-02-23 kinaba: double y = light[1]; 4fd800b3a8 2011-02-23 kinaba: double z = light[2]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if(x1==x2 && y1==y2 || y1==y2 && z1==z2 || z1==z2 && x1==x2 ) 4fd800b3a8 2011-02-23 kinaba: return 0; // line 4fd800b3a8 2011-02-23 kinaba: if(x1==x2 && x==x1 || y1==y2 && y==y1 || z1==z2 && z==z1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; // seen as a line 4fd800b3a8 2011-02-23 kinaba: if( y1 >= y ) 4fd800b3a8 2011-02-23 kinaba: return 0; // tree above light 4fd800b3a8 2011-02-23 kinaba: if( y2 >= y ) 4fd800b3a8 2011-02-23 kinaba: return -1; // infinite 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // otherwise project all vertices two y=0 plain 4fd800b3a8 2011-02-23 kinaba: vector<pt> p; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<8; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double X=(i&1 ? x1 : x2); 4fd800b3a8 2011-02-23 kinaba: double Y=(i&2 ? y1 : y2); 4fd800b3a8 2011-02-23 kinaba: double Z=(i&4 ? z1 : z2); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // (Xp-X) : (x-X) = (0-Y) : (y-Y) 4fd800b3a8 2011-02-23 kinaba: double Xp = (X-x)*Y/(y-Y) + X; 4fd800b3a8 2011-02-23 kinaba: double Zp = (Z-z)*Y/(y-Y) + Z; 4fd800b3a8 2011-02-23 kinaba: p.push_back( pt(Xp,Zp) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // calc convex hull and its area 4fd800b3a8 2011-02-23 kinaba: p = convex_hull(p); 4fd800b3a8 2011-02-23 kinaba: return ::area(p); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { int Arr0[] = {1,1,1, 10,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,5,5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 0.0; verify_case(0, Arg2, area(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arr0[] = {1,3,1, 10,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = -1.0; verify_case(1, Arg2, area(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arr0[] = {1,1,1, 2,2,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3,3,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 15.75; verify_case(2, Arg2, area(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { int Arr0[] = {1,1,1, 3,3,3} ; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = -1.0; verify_case(3, Arg2, area(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { Shadow().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE