原题:
There are n rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle A with the bottom left corner located at (0,0) and the top right corner at (2,2), and the other rectangle B with the bottom left corner located at (1,1) and the top right corner at (3,3), it follows that the area of the union of A and B should be 7, instead of 8.
Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.
Note:
(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,000.
(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.
Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 1000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b), and the top right corner of the rectangle is located at (c,d). Note that integers a, b, c, d can be as large as 1,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0 (zero) signifies the end of input.
For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.
2 0 0 2 2 1 1 3 3 3 0 0 1 1 2 2 3 3 4 4 5 5 0
7 3 *
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 5000;
const int INF = 0x3f3f3f3f;
int N, a[MAXN], X[MAXN];
struct MAP {
int x, id, lr;
bool operator < (const MAP &temp) const {
return x < temp.x;
}
}Map[MAXN];
struct MAT {
int x1, y1, x2, y2;
}Mat[MAXN];
struct Line {
int l, r, h, rc;
bool operator < (const Line &temp) const {
return h < temp.h;
}
}line[MAXN];
int main() {
while (~scanf("%d", &N) && N) {
int cnt = 0;
for (int i = 1; i <= N; i++) {
scanf("%d %d %d %d", &Mat[i].x1, &Mat[i].y1, &Mat[i].x2, &Mat[i].y2);
cnt++;
Map[cnt].x = Mat[i].x1, Map[cnt].id = i, Map[cnt].lr = 0;
cnt++;
Map[cnt].x = Mat[i].x2, Map[cnt].id = i, Map[cnt].lr = 1;
}
sort(Map + 1, Map + 1 + cnt);
int num = 0; Map[0].x = -INF;
for (int i = 1; i <= cnt; i++) {
if (Map[i].x != Map[i - 1].x) ++num;
int id = Map[i].id, lr = Map[i].lr;
if (lr == 0) Mat[id].x1 = num;
else Mat[id].x2 = num;
}
for (int i = 1; i <= cnt; i++) {
X[i] = Map[i].x;
}
unique(X + 1, X + 1 + cnt);
cnt = 0;
for (int i = 1; i <= N; i++) {
++cnt;
line[cnt].l = Mat[i].x1, line[cnt].r = Mat[i].x2, line[cnt].h = Mat[i].y1, line[cnt].rc = 0;
++cnt;
line[cnt].l = Mat[i].x1, line[cnt].r = Mat[i].x2, line[cnt].h = Mat[i].y2, line[cnt].rc = 1;
}
sort(line + 1, line + 1 + cnt);
memset(a, 0, sizeof(a));
ll ans = 0;
for (int i = 1; i < cnt; i++) {
int h = line[i + 1].h - line[i].h;
if (line[i].rc == 0) {
for (int j = line[i].l; j < line[i].r; j++) a[j]++;
}
else {
for (int j = line[i].l; j < line[i].r; j++) a[j]--;
}
num = 0;
for (int j = 1; j <= (N<<1) + 10; j++) {
if (a[j]) {
int k = j;
while (a[j]) j++;
num += X[j] - X[k];
}
}
ans += (ll)h * num;
}
printf("%lld\n", ans);
}
printf("*\n");
return 0;
}
//1 4 4 10
//3 8 9 12
//6 5 10 10
//3 1 8 6