Overlapping Rectangles(线段树,矩形面积并)
来源:互联网 发布:举报网络赌钱有奖励吗 编辑:程序博客网 时间:2024/05/24 04:07
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 <cctype>#include <algorithm>using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1#define LL long longconst int maxn = 2222;LL cnt[maxn << 2];LL sum[maxn << 2];LL X[maxn];struct Seg { LL h,l,r; int s; Seg(){} Seg(LL a,LL b,LL c,LL d) : l(a) , r(b) , h(c) , s(d) {} bool operator < (const Seg &cmp) const { return h < cmp.h; }}ss[maxn];void PushUp(int rt,int l,int r) { if (cnt[rt]) sum[rt] = X[r+1] - X[l]; else if (l == r) sum[rt] = 0; else sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void update(int L,int R,int c,int l,int r,int rt) { if (L <= l && r <= R) { cnt[rt] += c; PushUp(rt , l , r); return ; } int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); PushUp(rt , l , r);}int Bin(LL key,LL n,LL X[]) { int l = 0 , r = n - 1; while (l <= r) { int m = (l + r) >> 1; if (X[m] == key) return m; if (X[m] < key) l = m + 1; else r = m - 1; } return -1;}int main() { int n , cas = 1; while (~scanf("%d",&n) && n) { int m = 0; while (n --) { int a , b , c , d; scanf("%d%d%d%d",&a,&b,&c,&d); X[m] = a; ss[m++] = Seg(a , c , b , 1); X[m] = c; ss[m++] = Seg(a , c , d , -1); } sort(X , X + m); sort(ss , ss + m); int k = 1; for (int i = 1 ; i < m ; i ++) { if (X[i] != X[i-1]) X[k++] = X[i]; } memset(cnt , 0 , sizeof(cnt)); memset(sum , 0 , sizeof(sum)); int ret = 0; for (int i = 0 ; i < m - 1 ; i ++) { int l = Bin(ss[i].l , k , X); int r = Bin(ss[i].r , k , X) - 1; if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1); ret += sum[1] * (ss[i+1].h - ss[i].h); } printf("%d\n",ret); } printf("*"); return 0;}
- Overlapping Rectangles(线段树,矩形面积并)
- 2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)
- 矩形面积并集,2017ICPC网络赛(南宁)Overlapping Rectangles
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles (面积并+线段树)
- 矩形面积并-线段树
- 矩形面积并、矩形面积交、矩形周长并(线段树、扫描线总结)
- 矩形面积并、矩形面积交、矩形周长并(线段树、扫描线总结)
- HDOJ 2056 Rectangles(坐标排序、矩形面积并)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles(面积并)
- 线段树 (矩形面积并&&周长并 - 来自notonlysuccess)
- HDU1542 Atlantis(扫描线+矩形面积并+线段树)
- HDU 1542 Atlantis(线段树求矩形面积并)
- hdu 1542 矩形面积并(扫描线+线段树)
- 求矩形并的面积(线段树)【模板】
- HDU 1542 Atlantis(线段树求矩形面积并)
- POJ 1389 求矩形面积并(线段树)
- hdu 1542 (线段树求矩形面积并)
- 线段树 求矩形并 面积
- JavaSwing_4.10: JInternalFrame(内部窗口)
- 判断两个链表是否相交
- 希尔排序及代码实现
- 使用虚拟机实现lvs nat模式负载均衡
- 求旋转数组中的最小值
- Overlapping Rectangles(线段树,矩形面积并)
- 多线程的三种实现方式及比较
- 嵌入式硬件系统相关知识学习笔记
- 动态规划(0-1背包问题)---C#版
- [LeetCode][105,106] Construct Binary Tree from Inorder and (Post/Pre)order Traversal
- jq代码学习2——《锋利的JQUERY》 dom对象和jQ对象转换
- 各浏览器的用户代理字符串整理
- C语言字节对齐
- STM32-外部中断学习笔记