2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
来源:互联网 发布:java电子邮箱格式校验 编辑:程序博客网 时间:2024/05/29 23:25
原题:
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.
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 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.
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.
样例输入
20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50
样例输出
73*
#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("%lld\n", 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(线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles(面积并)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F.Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题 Overlapping Rectangles(线段树)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation 和 F. Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Overlapping Rectangles
- 2017 ACM-ICPC亚洲区(南宁赛区)网络赛 F.Frequent Subsets Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
- 编写优质嵌入式C程序
- Netty实战读书笔记三:EventLoop和线程模型
- CUDA中Block大小的选择
- 32位电脑和64位电脑的区别
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles
- C++百度笔试题
- 中文数据库要注意的点
- Linux cat 命令
- 分类管理 商品管理 订单管理 权限校验(AOP思想) 网上商城(五)
- 用指针对二维数组进行计算
- CentOS下下载安装Redis,楼主亲测
- Leetcode: 15. 3Sum(Week3,Medium)
- python 排序算法之---插入排序