Artifact Content
Not logged in

Artifact 1922da4e015e264ce59b4d6bbecd65aca1a05c7e


#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;

typedef pair<int,int> pt;
typedef pair<int,int> range;

class HiddenSquares {
public:
	int count(vector <int> x1, vector <int> y1, vector <int> x2, vector <int> y2) 
	{
		vector< pair<int, range> > xseg, yseg;
		for(int i=0; i<x1.size(); ++i)
		{
			xseg.push_back( make_pair(x1[i], range(y1[i], y2[i])) );
			xseg.push_back( make_pair(x2[i], range(y1[i], y2[i])) );
			yseg.push_back( make_pair(y1[i], range(x1[i], x2[i])) );
			yseg.push_back( make_pair(y2[i], range(x1[i], x2[i])) );
		}

		// copy and
		map<int, vector<range> > xray;
		for(int i=0; i<xseg.size(); ++i)
		{
			set<int> p;

			p.insert(xseg[i].second.first);
			p.insert(xseg[i].second.second);
			for(int j=0; j<yseg.size(); ++j)
				if( xseg[i].second.first<=yseg[j].first && yseg[j].first<=xseg[i].second.second
				 && yseg[j].second.first<=xseg[i].first && xseg[i].first<=yseg[j].second.second )
					p.insert( yseg[j].first );

			int py = xseg[i].second.first;
			for(set<int>::iterator y=p.begin(); ++y!=p.end(); py=*y)
				xray[xseg[i].first].push_back( range(py,*y) );
		}

		// paset!
		map<int, vector<range> > yray;
		for(int i=0; i<yseg.size(); ++i)
		{
			set<int> p;

			p.insert(yseg[i].second.first);
			p.insert(yseg[i].second.second);
			for(int j=0; j<xseg.size(); ++j)
				if( yseg[i].second.first<=xseg[j].first && xseg[j].first<=yseg[i].second.second
				 && xseg[j].second.first<=yseg[i].first && yseg[i].first<=xseg[j].second.second )
					p.insert( xseg[j].first );

			int px = yseg[i].second.first;
			for(set<int>::iterator x=p.begin(); ++x!=p.end(); px=*x)
				yray[yseg[i].first].push_back( range(px,*x) );
		}

		// copy and
		map< pt, set<pt> > xconn;
		for(map<int, vector< pt > >::iterator it=xray.begin(); it!=xray.end(); ++it)
		{
			int            x = it->first;
			vector<range>& r = it->second;

			vector< pair<int,bool> > es;
			for(int i=0; i!=r.size(); ++i)
				es.push_back( make_pair(r[i].first,false) ),
				es.push_back( make_pair(r[i].second,true) );
			sort(es.begin(), es.end());
			vector<int> cur;
			int nest = 0;
			for(int i=0; i!=es.size(); ++i)
			{
				cur.push_back( es[i].first );
				es[i].second ? --nest : ++nest;
				if( nest == 0 )
				{
					for(int j=0; j<cur.size(); ++j)
						for(int k=j+1; k<cur.size(); ++k)
							if( cur[j] < cur[k] )
								xconn[pt(x,cur[j])].insert( pt(x,cur[k]) );
					cur.clear();
				}
			}
		}

		// paste!
		map< pt, set<pt> > yconn;
		for(map<int, vector< pt > >::iterator it=yray.begin(); it!=yray.end(); ++it)
		{
			int            y = it->first;
			vector<range>& r = it->second;

			vector< pair<int,bool> > es;
			for(int i=0; i!=r.size(); ++i)
				es.push_back( make_pair(r[i].first,false) ),
				es.push_back( make_pair(r[i].second,true) );
			sort(es.begin(), es.end());

			vector<int> cur;
			int nest = 0;
			for(int i=0; i!=es.size(); ++i)
			{
				cur.push_back( es[i].first );
				es[i].second ? --nest : ++nest;
				if( nest == 0 )
				{
					for(int j=0; j<cur.size(); ++j)
						for(int k=j+1; k<cur.size(); ++k)
							if( cur[j] < cur[k] )
								yconn[pt(cur[j],y)].insert( pt(cur[k],y) );
					cur.clear();
				}
			}
		}

		// count squares
		int cnt = 0;
		for(map< pt, set<pt> >::iterator it=xconn.begin(); it!=xconn.end(); ++it)
		{
			pt       p = it->first;
			set<pt>& c = it->second;
			for(set<pt>::iterator jt=c.begin(); jt!=c.end(); ++jt)
			{
				pt q = *jt;
				int w = q.second-p.second;
				pt p1(p.first+w,p.second);
				pt q1(q.first+w,q.second);
				if( yconn[p].count(p1) && yconn[q].count(q1) && xconn[p1].count(q1) )
					++cnt;
			}
		}
		return cnt;
	}
};

// 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> 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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int x1_[] = {0,1,0};
	  vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 
	int y1_[] = {0,0,1};
	  vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 
	int x2_[] = {3,2,3};
	  vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 
	int y2_[] = {3,3,2};
	  vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 
	int RetVal = 14; 
	return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); }
int Test_(Case_<1>) {
	int x1_[] = {0,2};
	  vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 
	int y1_[] = {0,0};
	  vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 
	int x2_[] = {2,4};
	  vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 
	int y2_[] = {4,4};
	  vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 
	int RetVal = 1; 
	return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); }
int Test_(Case_<2>) {
	int x1_[] = {0,0,3};
	  vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 
	int y1_[] = {0,3,0};
	  vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 
	int x2_[] = {1,4,4};
	  vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 
	int y2_[] = {3,4,3};
	  vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 
	int RetVal = 0; 
	return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); }
int Test_(Case_<3>) {
	int x1_[] = {453453463,453453500,453453495,453453512,453453478,453453489,
 453453466,453453500,453453498,453453510,453453472,453453512,
 453453519,453453514,453453521,453453518,453453523,453453517,
 453453466,453453525,453453496,453453495,453453463,453453461,
 453453460,453453522,453453471,453453468,453453479,453453517,
 453453485,453453518,453453499,453453464,453453494};
	  vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 
	int y1_[] = {364646438,364646402,364646449,364646438,364646415,364646401,
 364646446,364646416,364646456,364646414,364646463,364646407,
 364646436,364646450,364646421,364646411,364646414,364646419,
 364646445,364646427,364646405,364646442,364646418,364646464,
 364646457,364646463,364646432,364646426,364646444,364646431,
 364646450,364646418,364646434,364646458,364646402};
	  vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); 
	int x2_[] = {453453488,453453510,453453525,453453523,453453493,453453500,
 453453470,453453511,453453499,453453521,453453518,453453524,
 453453523,453453523,453453524,453453523,453453525,453453519,
 453453473,453453526,453453511,453453517,453453470,453453464,
 453453511,453453524,453453516,453453516,453453492,453453524,
453453513,453453522,453453520,453453505,453453512};
	  vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 
	int y2_[] = {364646460,364646454,364646462,364646466,364646464,364646455,
 364646457,364646461,364646457,364646450,364646466,364646453,
 364646441,364646451,364646460,364646461,364646446,364646464,
 364646447,364646460,364646454,364646450,364646444,364646466,
 364646458,364646466,364646455,364646442,364646462,364646435,
 364646464,364646444,364646455,364646459,364646430};
	  vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); 
	int RetVal = 124; 
	return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE