Artifact Content
Not logged in

Artifact 86534a42ab0996e3ec636ced73823a16eefc3627


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

class PolylineAreas { public:
	vector<long long> findAreas(string polyline)
	{
		string cmd;
		{
			const char* p = polyline.c_str();
			while (*p)
				parse_one(p, cmd);
		}

		#define enc(x,y) ((LL(x+1000000)<<32)|(y+1000000))
		const int dx[] = { 1, 0, -1, 0 };
		const int dy[] = { 0, 1, 0, -1 };

		unordered_map<LL, int> edge;
		{
			int x = 0, y = 0, d = 0;
			for (int ch : cmd) {
				switch (ch) {
				case 'F': {
					LL p = enc(x, y);
					x += dx[d];
					y += dy[d];
					LL c = enc(x, y);
					edge[p] |= 1 << d;
					edge[c] |= 1 << ((d + 2) % 4);

					break;
				}
				case 'L': d = (d + 1) % 4; break;
				case 'R': d = (d + 3) % 4; break;
				}
			}
		}

		vector<LL> ans;
		{
			for (auto pd : edge) {
				const int s_x = int(pd.first >> 32) - 1000000;
				const int s_y = int(pd.first & 0xffffffff) - 1000000;
				int& ds = pd.second;
				for (int d = 0; d < 4; ++d) if (ds&(1 << d)) {
					// go clockwise from (x,y) -> d

					LL area = 0;
					int x = s_x, y = s_y;
					do {
						//cerr << "(" << x << "," << y << "): " << d << endl;

						// move
						int px = x, py = y;
						x += dx[d];
						y += dy[d];
						edge[enc(px, py)] &= ~(1 << d);

						// turn
						int& dd = edge[enc(x, y)];
						if (dd & (1 << ((d + 3) % 4)))
							d = (d + 3) % 4;
						else if (dd & (1 << d))
							;
						else if (dd & (1 << ((d + 1) % 4)))
							d = (d + 1) % 4;
						else if (dd & (1 << ((d + 2)%4)))
							d = (d + 2) % 4;
						else
							break;

						// calc area
						area += LL(x - s_x)*(py - s_y) - LL(px-s_x)*(y-s_y);
						//cerr << LL(x - s_x)*(py - s_y) - LL(px - s_x)*(y - s_y) << endl;
					} while (x != s_x || y != s_y);

					if(x==s_x && y==s_y && area > 0)
						ans.push_back(area/2);
				}
			}
		}

		sort(ans.begin(), ans.end());
		if (ans.size() > 200) {
			vector<LL> a2(ans.begin(), ans.begin() + 100);
			a2.insert(a2.end(), ans.end() - 100, ans.end());
			return a2;
		}
		return ans;
	}

	void parse_one(const char*& p, string& buf) {
		if ('0' <= *p && *p <= '9') {
			int rep = (*p++ - '0');
			while('0' <= *p && *p <= '9')
				rep = rep*10 + (*p++ - '0');

			string sub;
			parse_one(p, sub);
			while (rep-- > 0)
				buf += sub;
		}
		else if (*p == '[') {
			for (++p; *p != ']'; )
				parse_one(p, buf);
			++p;
		}
		else {
			buf += *p++;
		}
	}
};

// 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 vector<long long>& Expected, const vector<long long>& Received) {
 bool ok = (Expected == Received);
 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(_, PolylineAreas().findAreas(polyline));}
int main(){
CASE(0)
	string polyline = "FRFLF"; 
	vector<long long> _; 
END
CASE(1)
	string polyline = "4[100FR]"; 
	long long __[] = {10000 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	string polyline = "2[2[100FR]]"; 
	long long __[] = {10000 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	string polyline = "47[100FR]"; 
	long long __[] = {10000 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	string polyline = "1000000[]"; 
	vector<long long> _; 
END
CASE(5)
	string polyline = "4[6FR]FR6FL2FL6FR3FRFR6FL2FL6F"; 
	long long __[] = {1, 2, 2, 3, 3, 4, 6, 6, 9 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(6)
	string polyline = "4[6FR]FR6FL2FL6FR3FRFR6FL2FL4F"; 
	long long __[] = {1, 2, 2, 3, 3, 4, 6, 15 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(7)
	string polyline = "3FRFRFLFLFRFR3FRFRFLFLFRF"; 
	long long __[] = {7 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(8)
	string polyline = "250000F"; 
    long long __[] = {-1};
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(9)
	string polyline = ; 
	long long __[] = ;
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE