Artifact Content
Not logged in

Artifact 52ba9270e5567c22ca5d4c8e2066ed0a601662f7


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class RectangularObstacle { public:
	long long countReachable(int x1_, int x2_, int y1_, int y2_, int s_)
	{
		LL x1 = x1_, x2 = x2_, y1 = y1_, y2 = y2_, s=s_;
		LL answer = 0;

		answer += pyramid(s, s);
		answer += 2 * s + 1;
		answer += pyramid(s, min(s, y2));

		for (LL y=y1; y<=y2 && y<=s; ++y) {
		}
		// [-s, -1]
		//    ==> 1 + 3 + 5 + .. + (2*s-1)
		answer += (1LL + 2*s - 1) * s / 2;

		// [0, y1-1]
		//    ==> (2*s+1) + (2*s-1) + (2*s-3) + ...
		LL ymax_case2 = min(y1 - 1, s);
		answer += (2 * s + 1 + 2 * (s - ymax_case2) + 1) * (ymax_case2 + 1) / 2;
		cerr << answer << endl;
		if (s < y1)
			return answer;

		// [y1, y2]
		//    ==> normal, but [x1,x2] subtracted
		answer += case3(s - y1, x2, y2 - y1 + 1);
		answer += case3(s - y1, -x1, y2 - y1 + 1);
		cerr << answer << endl;
		if (s <= y2)
			return answer;

		// [y2+1, +s] : min
		//    ==> (0,0) -> (0, y1-1) -> (x2+1, y1-1) -> (x2+1, y) -> (x,y)
		//    ==> (0,0) -> (0, y1-1) -> (x1-1, y1-1) -> (x2+1, y) -> (x,y)
		LL ls = -x1 + 1 + y2 + 1;  // (x1-1, y2+1)
		LL rs = x2 + 1 + y2 + 1;   // (x2+1, y2+1)
		answer += case4(x1 - 1, s - ls, x2 + 1, s - rs);
		return answer;
	}

	static LL pyramid(LL s, LL h) {
		return (2 * s - 1 + 2 * s - 1 - 2 * h) * h / 2;
	}

	static LL case3(LL len, LL obs, LL cnt) {
		cerr << len << " " << obs << " " << cnt << endl;
		cnt = max(0LL, min(cnt, len - obs));
		cerr << (len-obs + len-obs - (cnt - 1)) * cnt / 2 << endl;
		return (len + len - (cnt - 1)) * cnt / 2;
	}

	static LL case4(LL x1, LL s1, LL x2, LL s2) {
		cerr << x1 << " " << s1 << " " << x2 << " " << s2 << endl;
		LL answer = 0;
		if (s1 >= 0) answer += simp(s1);
		if (s2 >= 0) answer += simp(s2);
		if (s1 >= 0 && s2 >= 0) {
			LL xl = x2 - s2;
			LL xr = x1 + s1;
			if (xl <= xr) {
				LL w = xr - xl + 1;
				answer -= (w + w%2) * (w/2+1) / 2;
			}
		}
		return answer;
	}

	static LL simp(LL s) {
		return (1 + 2 * s + 1) * (s + 1) / 2;
	}
};

// 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 long long& Expected, const 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(_, RectangularObstacle().countReachable(x1, x2, y1, y2, s));}
int main(){

CASE(0)
	int x1 = -5; 
	int x2 = 5; 
	int y1 = 3; 
	int y2 = 3; 
	int s = 2; 
	long long _ = 13LL; 
END
CASE(1)
	int x1 = -5; 
	int x2 = 5; 
	int y1 = 3; 
	int y2 = 3; 
	int s = 3; 
	long long _ = 24LL; 
END
CASE(2)
	int x1 = -4; 
	int x2 = 1; 
	int y1 = 1; 
	int y2 = 2; 
	int s = 6; 
	long long _ = 61LL; 
END
CASE(3)
	int x1 = 0; 
	int x2 = 0; 
	int y1 = 1; 
	int y2 = 1; 
	int s = 4; 
	long long _ = 38LL; 
END
CASE(4)
	int x1 = -100; 
	int x2 = 100; 
	int y1 = 42; 
	int y2 = 47; 
	int s = 0; 
	long long _ = 1LL; 
END
CASE(5)
	int x1 = -1000000; 
	int x2 = +1000000; 
	int y1 = +500000; 
	int y2 = +1000000; 
	int s = +1000000000; 
	long long _ = -1LL; 
END
CASE(6)
	int x1 = -0; 
	int x2 = +0; 
	int y1 = 1; 
	int y2 = 1;
	int s = 2;
	long long _ = -1LL; 
END
}
// END CUT HERE