hdu3265-Posters 线段树+离散化 求矩形面积并
2017-07-24 19:49
288 查看
Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6327 Accepted Submission(s): 1520
Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters.
However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap
when he pastes them on the window.
Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.
To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the
posters and the edges of the holes on the posters are all parallel with the coordinate axes.
Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about
one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right
corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.
The input ends with a line of single zero.
Output
For each test case, output a single line with the total area of window covered by posters.
Sample Input
2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7
0
Sample Output
56
题目大意:给你一些矩形,从中间扣掉矩形贴在窗户上,求最后这些贴的地方的面积,即求面积并。
解题思路:这道题和 hdu1542求面积并很像,只是这里的矩形变成了一个中间有矩形空洞的矩形,所以我们把这个矩形变成4个矩形就可以了,然后再用相同的方法就可以求出总的面积并了。
ac代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define lz 2*u,l,mid #define rz 2*u+1,mid+1,r const int maxn=50005; typedef long long lld; lld sum[4*maxn]; int flag[4*maxn]; struct Node { int lx, rx, h, s; Node(){} Node(int lx_, int rx_, int h_, int s_) { lx=lx_, rx=rx_, h=h_, s=s_; } bool operator<(const Node &S)const { return h<S.h; } }line[8*maxn]; void push_up(int u, int l, int r) { if(flag[u]) sum[u]=r-l+1; else if(l==r) sum[u]=0; else sum[u]=sum[2*u]+sum[2*u+1]; } void Update(int u, int l, int r, int tl, int tr, int c) { if(tl>tr) return ; ///!!!注意 当x1==x2时,会导致RE,因为算区间的时候x2会减-1 if(tl<=l&&r<=tr) { flag[u]+=c; push_up(u,l,r); return ; } int mid=(l+r)>>1; if(tr<=mid) Update(lz,tl,tr,c); else if(tl>mid) Update(rz,tl,tr,c); else { Update(lz,tl,mid,c); Update(rz,mid+1,tr,c); } push_up(u,l,r); } int main() { int n; while(cin >> n,n) { memset(flag,0,sizeof(flag)); memset(sum,0,sizeof(sum)); int num=0; int x1, y1, x2, y2, x3, y3, x4, y4; int lbd=maxn, rbd=-1; for(int i=0; i<n; i++) { scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x4,&y4,&x2,&y2,&x3,&y3); line[++num]=Node(x1,x4,y1,1); line[++num]=Node(x1,x4,y2,-1); line[++num]=Node(x1,x4,y3,1); line[++num]=Node(x1,x4,y4,-1); line[++num]=Node(x1,x2,y2,1); line[++num]=Node(x1,x2,y3,-1); line[++num]=Node(x3,x4,y2,1); line[++num]=Node(x3,x4,y3,-1); lbd=min(x1,lbd); rbd=max(x4,rbd); } sort(line+1,line+num+1); lld ans=0; for(int i=1; i<num; i++) { Update(1,lbd,rbd,line[i].lx,line[i].rx-1,line[i].s); ans+=sum[1]*(lld)(line[i+1].h-line[i].h); //要使用long long } printf("%lld\n", ans); } return 0; }
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=3265
hdu1542解题报告:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75647032
线段树专题网址:点击打开链接http://blog.csdn.net/wang_heng199/article/details/74938672
相关文章推荐
- HDU3265 Posters(线段树,扫描线,矩形面积并)
- 扫描线 求挖空矩形合并面积 hdu3265 Posters
- codevs 3044 线段树+离散化+扫描线 (矩形面积并
- poj 1151 线段树+离散化+扫描线 矩形面积并 (矩阵操作类)
- ACM HDU 1255 覆盖的面积(矩形面积交,线段树+离散化)
- poj 1151 线段树+离散化+扫描线 (矩形面积并
- HDU-1542/POJ-1151 Atlantis(矩形并面积--线段树+离散化)
- UESTC 1600 艾尔大停电 线段树+扫描线、矩形面积并、浮点数的离散化
- HDU1542 POJ1151矩形面积合并(线段树+离散化)
- hdu 1255 线段树+离散化+扫描线 (矩形面积交
- poj 1151 Atlantis(矩形面积并,线段树+离散化)
- HDU 1542 Atlantis(矩形面积并,线段树+离散化+线扫描)
- POJ1151 (HDU 1542) Atlantis【矩形面积并,线段树+离散化+扫描线模板】
- HDU1255 覆盖的面积(线段树,离散化,扫描线,矩形面积交)
- POJ 2528 Mayor's posters --线段树+离散化
- poj 1177 线段树+离散化+扫描线 求矩形并的轮廓长
- poj1151 Atlantis(线段树+扫描线+离散化,求矩形面积并)
- hdu 3265 Posters(矩形面积并 扫描线+线段树)
- 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]