我是靠谱客的博主 和谐热狗,最近开发中收集的这篇文章主要介绍2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原题:

There are nn 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 AA with the bottom left corner located at (0, 0)(0,0) and the top right corner at (2, 2)(2,2), and the other rectangle BB with the bottom left corner located at (1,1)(1,1) and the top right corner at (3,3)(3,3), it follows that the area of the union of AA and BB should be 77, instead of 88.

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,0001,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.

Input Format

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 10001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a, b)(a,b), and the top right corner of the rectangle is located at (c, d)(c,d). Note that integers aabbccdd can be as large as 1,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.

Output Format

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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
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("%lldn", ans);
}
printf("*n");
return 0;
}
//1 4 4 10
//3 8 9 12
//6 5 10 10
//3 1 8 6


最后

以上就是和谐热狗为你收集整理的2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles的全部内容,希望文章能够帮你解决2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部