Cod sursa(job #1740646)

Utilizator vladc096Vlad Cincean vladc096 Data 11 august 2016 22:34:06
Problema Reuniune Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <cstdio>
#include <cstdlib>

typedef struct {
	long long x0 = 0;
	long long y0 = 0;
	long long x1 = 0;
	long long y1 = 0;
} rectangle;

inline long long max(long long a, long long b) {
	return a > b ? a : b;
}

inline long long min(long long a, long long b) {
	return a < b ? a : b;
}

inline long long area(rectangle r) {
	return abs(r.x0 - r.x1) * abs(r.y0 - r.y1);
}

inline long long perimeter(rectangle r) {
	return 2 * abs(r.x0 - r.x1) + 2 * abs(r.y0 - r.y1);
}

inline rectangle intersect(rectangle r1, rectangle r2) {
	rectangle ret;

	// chech if intersection is non-empty
	if (r1.x0 < r2.x1 && r1.x1 > r2.x0 && r1.y0 < r2.y1 && r1.x1 > r2.x0) {
		ret.x0 = min(max(r1.x0, r2.x0), r2.x1);
		ret.y0 = min(max(r1.y0, r2.y0), r2.y1);
		ret.x1 = min(max(r1.x0, r2.x1), r1.x1);
		ret.y1 = min(max(r1.y0, r2.y1), r1.y1);
	}
	return ret;
}

inline rectangle intersect(rectangle r1, rectangle r2, rectangle r3) {
	return intersect(intersect(r1, r2), r3);
}

int main() {
	rectangle R[3];
	int i;
	long long A, P;

	freopen("reuniune.in", "r", stdin);
	freopen("reuniune.out", "w", stdout);

	// input
	for (i = 0; i < 3; ++i) {
		scanf("%lld %lld %lld %lld", &R[i].x0, &R[i].y0, &R[i].x1, &R[i].y1);
	}

	// solve
	A = area(R[0]) + area(R[1]) + area(R[2]);
	A -= area(intersect(R[0], R[1]));
	A -= area(intersect(R[0], R[2]));
	A -= area(intersect(R[1], R[2]));
	A += area(intersect(R[0], R[1], R[2]));

	P = perimeter(R[0]) + perimeter(R[1]) + perimeter(R[2]);
	P -= perimeter(intersect(R[0], R[1]));
	P -= perimeter(intersect(R[0], R[2]));
	P -= perimeter(intersect(R[1], R[2]));
	P += perimeter(intersect(R[0], R[1], R[2]));

	// output
	printf("%lld %lld\n", A, P);

	return 0;
}