博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Overlapping Rectangles
阅读量:4693 次
发布时间:2019-06-09

本文共 4555 字,大约阅读时间需要 15 分钟。

There are nnn 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 AAA with the bottom left corner located at (0,0) and the top right corner at (2,2), and the other rectangleB 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 aaa, bbb, ccc, ddd can be as large as 1,000,0001,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0n = 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.

样例输入

20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50

样例输出

73*

题目来源

线段是之扫描线的魔板题

1 #include 
2 using namespace std; 3 4 #define lz 2*u,l,mid 5 #define rz 2*u+1,mid+1,r 6 const int N=10000; 7 int sum[N]; 8 int flag[N]; 9 int X[N];10 11 struct Node {12 int lx, rx, y;13 int s;14 Node(){};15 Node(int lx_, int rx_, int y_, int s_) {16 lx = lx_, rx = rx_, y = y_, s = s_;17 }18 bool operator < (const Node &S) const {19 return y < S.y;20 }21 }line[N];22 23 int find(int tmp, int n) {24 int l = 1, r = n, mid;25 while(l <= r) {26 mid = (l+r) >> 1;27 if(X[mid] == tmp) return mid;28 else if(X[mid] < tmp) l = mid+1;29 else r = mid-1;30 }31 }32 33 void push_up(int u, int l, int r) {34 if(flag[u]) sum[u] = X[r+1]-X[l];35 else if(l == r) sum[u] = 0;36 else sum[u] = sum[2*u] + sum[2*u+1];37 }38 39 void Update(int u, int l, int r, int tl, int tr, int c) {40 if(tl <= l&&r <= tr) {41 flag[u] += c;42 push_up(u,l,r);43 return ;44 }45 int mid = (l + r) >> 1;46 if(tr <= mid) Update(lz, tl, tr, c);47 else if(tl > mid) Update(rz, tl, tr, c);48 else {49 Update(lz, tl, mid, c);50 Update(rz, mid+1, tr, c);51 }52 push_up(u,l,r);53 }54 55 int main() {56 int n;57 while(scanf("%d", &n) && n) {58 int num=0;59 memset(flag, 0, sizeof(flag));60 memset(sum, 0, sizeof(sum));61 for(int i = 0; i < n; i ++) {62 int x1, x2, y1, y2;63 scanf("%d%d%d%d",&x1, &y1, &x2, &y2);64 line[++num] = Node(x1,x2,y1,1);65 X[num] = x1;66 line[++num] = Node(x1,x2,y2,-1);67 X[num] = x2;68 }69 sort(X + 1, X + num + 1);70 sort(line + 1,line + num + 1);71 int k = 1;72 for(int i = 2; i <= num; i ++)73 if(X[i] != X[i+1]) X[++k] = X[i];74 int ans = 0;75 for(int i = 1; i < num; i ++) {76 int l = find(line[i].lx, k);77 int r = find(line[i].rx, k) - 1;78 Update(1, 1, k, l, r, line[i].s);79 ans += sum[1] * (line[i+1].y - line[i].y);80 }81 printf("%d\n",ans);82 }83 printf("*\n");84 return 0;85 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/7588579.html

你可能感兴趣的文章
【C++算法与数据结构学习笔记------单链表实现多项式】
查看>>
C#垃圾回收机制
查看>>
31、任务三十一——表单联动
查看>>
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
23 Java学习之RandomAccessFile
查看>>
P2709 小B的询问
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
lock_sga引起的ksvcreate :process(m000) creation failed
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>