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 <functional> 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: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,int> pt; 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,int> range; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class HiddenSquares { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int count(vector <int> x1, vector <int> y1, vector <int> x2, vector <int> y2) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< pair<int, range> > xseg, yseg; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<x1.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: xseg.push_back( make_pair(x1[i], range(y1[i], y2[i])) ); 4fd800b3a8 2011-02-23 kinaba: xseg.push_back( make_pair(x2[i], range(y1[i], y2[i])) ); 4fd800b3a8 2011-02-23 kinaba: yseg.push_back( make_pair(y1[i], range(x1[i], x2[i])) ); 4fd800b3a8 2011-02-23 kinaba: yseg.push_back( make_pair(y2[i], range(x1[i], x2[i])) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // copy and 4fd800b3a8 2011-02-23 kinaba: map<int, vector<range> > xray; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<xseg.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<int> p; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: p.insert(xseg[i].second.first); 4fd800b3a8 2011-02-23 kinaba: p.insert(xseg[i].second.second); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<yseg.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( xseg[i].second.first<=yseg[j].first && yseg[j].first<=xseg[i].second.second 4fd800b3a8 2011-02-23 kinaba: && yseg[j].second.first<=xseg[i].first && xseg[i].first<=yseg[j].second.second ) 4fd800b3a8 2011-02-23 kinaba: p.insert( yseg[j].first ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int py = xseg[i].second.first; 4fd800b3a8 2011-02-23 kinaba: for(set<int>::iterator y=p.begin(); ++y!=p.end(); py=*y) 4fd800b3a8 2011-02-23 kinaba: xray[xseg[i].first].push_back( range(py,*y) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // paset! 4fd800b3a8 2011-02-23 kinaba: map<int, vector<range> > yray; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<yseg.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<int> p; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: p.insert(yseg[i].second.first); 4fd800b3a8 2011-02-23 kinaba: p.insert(yseg[i].second.second); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<xseg.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( yseg[i].second.first<=xseg[j].first && xseg[j].first<=yseg[i].second.second 4fd800b3a8 2011-02-23 kinaba: && xseg[j].second.first<=yseg[i].first && yseg[i].first<=xseg[j].second.second ) 4fd800b3a8 2011-02-23 kinaba: p.insert( xseg[j].first ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int px = yseg[i].second.first; 4fd800b3a8 2011-02-23 kinaba: for(set<int>::iterator x=p.begin(); ++x!=p.end(); px=*x) 4fd800b3a8 2011-02-23 kinaba: yray[yseg[i].first].push_back( range(px,*x) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // copy and 4fd800b3a8 2011-02-23 kinaba: map< pt, set<pt> > xconn; 4fd800b3a8 2011-02-23 kinaba: for(map<int, vector< pt > >::iterator it=xray.begin(); it!=xray.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int x = it->first; 4fd800b3a8 2011-02-23 kinaba: vector<range>& r = it->second; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,bool> > es; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: es.push_back( make_pair(r[i].first,false) ), 4fd800b3a8 2011-02-23 kinaba: es.push_back( make_pair(r[i].second,true) ); 4fd800b3a8 2011-02-23 kinaba: sort(es.begin(), es.end()); 4fd800b3a8 2011-02-23 kinaba: vector<int> cur; 4fd800b3a8 2011-02-23 kinaba: int nest = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=es.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: cur.push_back( es[i].first ); 4fd800b3a8 2011-02-23 kinaba: es[i].second ? --nest : ++nest; 4fd800b3a8 2011-02-23 kinaba: if( nest == 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<cur.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: for(int k=j+1; k<cur.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: if( cur[j] < cur[k] ) 4fd800b3a8 2011-02-23 kinaba: xconn[pt(x,cur[j])].insert( pt(x,cur[k]) ); 4fd800b3a8 2011-02-23 kinaba: cur.clear(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // paste! 4fd800b3a8 2011-02-23 kinaba: map< pt, set<pt> > yconn; 4fd800b3a8 2011-02-23 kinaba: for(map<int, vector< pt > >::iterator it=yray.begin(); it!=yray.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int y = it->first; 4fd800b3a8 2011-02-23 kinaba: vector<range>& r = it->second; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,bool> > es; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: es.push_back( make_pair(r[i].first,false) ), 4fd800b3a8 2011-02-23 kinaba: es.push_back( make_pair(r[i].second,true) ); 4fd800b3a8 2011-02-23 kinaba: sort(es.begin(), es.end()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> cur; 4fd800b3a8 2011-02-23 kinaba: int nest = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=es.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: cur.push_back( es[i].first ); 4fd800b3a8 2011-02-23 kinaba: es[i].second ? --nest : ++nest; 4fd800b3a8 2011-02-23 kinaba: if( nest == 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<cur.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: for(int k=j+1; k<cur.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: if( cur[j] < cur[k] ) 4fd800b3a8 2011-02-23 kinaba: yconn[pt(cur[j],y)].insert( pt(cur[k],y) ); 4fd800b3a8 2011-02-23 kinaba: cur.clear(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // count squares 4fd800b3a8 2011-02-23 kinaba: int cnt = 0; 4fd800b3a8 2011-02-23 kinaba: for(map< pt, set<pt> >::iterator it=xconn.begin(); it!=xconn.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: pt p = it->first; 4fd800b3a8 2011-02-23 kinaba: set<pt>& c = it->second; 4fd800b3a8 2011-02-23 kinaba: for(set<pt>::iterator jt=c.begin(); jt!=c.end(); ++jt) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: pt q = *jt; 4fd800b3a8 2011-02-23 kinaba: int w = q.second-p.second; 4fd800b3a8 2011-02-23 kinaba: pt p1(p.first+w,p.second); 4fd800b3a8 2011-02-23 kinaba: pt q1(q.first+w,q.second); 4fd800b3a8 2011-02-23 kinaba: if( yconn[p].count(p1) && yconn[q].count(q1) && xconn[p1].count(q1) ) 4fd800b3a8 2011-02-23 kinaba: ++cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 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: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {0,1,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int y1_[] = {0,0,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {3,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y2_[] = {3,3,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 14; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {0,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int y1_[] = {0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {2,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y2_[] = {4,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {0,0,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int y1_[] = {0,3,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {1,4,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y2_[] = {3,4,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {453453463,453453500,453453495,453453512,453453478,453453489, 4fd800b3a8 2011-02-23 kinaba: 453453466,453453500,453453498,453453510,453453472,453453512, 4fd800b3a8 2011-02-23 kinaba: 453453519,453453514,453453521,453453518,453453523,453453517, 4fd800b3a8 2011-02-23 kinaba: 453453466,453453525,453453496,453453495,453453463,453453461, 4fd800b3a8 2011-02-23 kinaba: 453453460,453453522,453453471,453453468,453453479,453453517, 4fd800b3a8 2011-02-23 kinaba: 453453485,453453518,453453499,453453464,453453494}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int y1_[] = {364646438,364646402,364646449,364646438,364646415,364646401, 4fd800b3a8 2011-02-23 kinaba: 364646446,364646416,364646456,364646414,364646463,364646407, 4fd800b3a8 2011-02-23 kinaba: 364646436,364646450,364646421,364646411,364646414,364646419, 4fd800b3a8 2011-02-23 kinaba: 364646445,364646427,364646405,364646442,364646418,364646464, 4fd800b3a8 2011-02-23 kinaba: 364646457,364646463,364646432,364646426,364646444,364646431, 4fd800b3a8 2011-02-23 kinaba: 364646450,364646418,364646434,364646458,364646402}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {453453488,453453510,453453525,453453523,453453493,453453500, 4fd800b3a8 2011-02-23 kinaba: 453453470,453453511,453453499,453453521,453453518,453453524, 4fd800b3a8 2011-02-23 kinaba: 453453523,453453523,453453524,453453523,453453525,453453519, 4fd800b3a8 2011-02-23 kinaba: 453453473,453453526,453453511,453453517,453453470,453453464, 4fd800b3a8 2011-02-23 kinaba: 453453511,453453524,453453516,453453516,453453492,453453524, 4fd800b3a8 2011-02-23 kinaba: 453453513,453453522,453453520,453453505,453453512}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y2_[] = {364646460,364646454,364646462,364646466,364646464,364646455, 4fd800b3a8 2011-02-23 kinaba: 364646457,364646461,364646457,364646450,364646466,364646453, 4fd800b3a8 2011-02-23 kinaba: 364646441,364646451,364646460,364646461,364646446,364646464, 4fd800b3a8 2011-02-23 kinaba: 364646447,364646460,364646454,364646450,364646444,364646466, 4fd800b3a8 2011-02-23 kinaba: 364646458,364646466,364646455,364646442,364646462,364646435, 4fd800b3a8 2011-02-23 kinaba: 364646464,364646444,364646455,364646459,364646430}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 124; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: