HDU3265 Posters(线段树+扫描线——面积交)

xiaoxiao2021-02-27  130

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6338    Accepted Submission(s): 1523 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

题意:给出n组矩形海报,每组在海报中间挖一个矩形的洞,求剩下的总面积

分析:首先想到用扫面线,对于一个矩形中间去掉一个矩形,那么我们可以把它分成四个小矩形,然后就敲一遍扫描线 坑点:需要注意的是中间的矩形和外边矩形重合的情况。什么?你说重合时更新的是一个点对结果无影响?     需要注意扫描线更新时为了更新m~m+1这一段,都在最右端-1,这样遇到l=r(重合时)的边,r-1后,更新       会出现r>l的情况,就RE了。。。 刚开始套的是离散化的代码,可以A但太难看,放在后面贴出 非离散化:530ms #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; const int N=50005; struct edge { int l,r,h; int f; edge() {} edge(int x1,int x2,int y,int op) { l=x1,r=x2,h=y,f=op; } bool operator < (const edge&A)const { return h<A.h; } } e[N<<3]; struct node { int cnt; ll s; } t[N<<2]; int pos[N]; void pushdown(int rt,int le,int ri) { if(t[rt].cnt) t[rt].s=ri-le+1; else if(le==ri) t[rt].s=0; else t[rt].s=t[rt<<1].s+t[rt<<1|1].s; } void update(int x,int y,int l,int r,int rt,int val) { if(x>y)return;//前面说过的情况,重合直接return,拒绝RE if(x<=l&&y>=r) { t[rt].cnt+=val; pushdown(rt,l,r); return; } int m=(l+r)>>1; if(x<=m) update(x,y,l,m,rt<<1,val); if(y>m) update(x,y,m+1,r,rt<<1|1,val); pushdown(rt,l,r); } int main() { int n,x1,x2,x3,x4,y1,y2,y3,y4; while(~scanf("%d",&n)&&n) { mem(t,0); int tot=0,lmin=N,rmax=-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); e[++tot]=edge(x1,x4,y1,1); e[++tot]=edge(x1,x4,y2,-1); e[++tot]=edge(x1,x4,y3,1); e[++tot]=edge(x1,x4,y4,-1); e[++tot]=edge(x1,x2,y2,1); e[++tot]=edge(x1,x2,y3,-1); e[++tot]=edge(x3,x4,y2,1); e[++tot]=edge(x3,x4,y3,-1); lmin=min(lmin,x1); rmax=max(rmax,x4); } sort(e+1,e+tot+1); ll ans=0; for(int i=1; i<tot; i++) { update(e[i].l,e[i].r-1,lmin,rmax,1,e[i].f); ans+=(e[i+1].h-e[i].h)*(ll)t[1].s; } printf("%lld\n",ans); } return 0; } 离散化:748ms #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int N=50005; struct edge { int l,r,h; int f; edge() {} edge(int x1,int x2,int y,int op) { l=x1,r=x2,h=y,f=op; } bool operator < (const edge&A)const { return h<A.h; } } e[N<<3]; struct node { int cnt; ll s; } t[N<<2]; int pos[N<<3]; void pushdown(int rt,int le,int ri) { if(t[rt].cnt) t[rt].s=pos[ri+1]-pos[le]; else if(le==ri) t[rt].s=0; else t[rt].s=t[rt<<1].s+t[rt<<1|1].s; } void update(int x,int y,int l,int r,int rt,int val) { if(x>y)return;//前面说过的情况,重合直接return,拒绝RE if(x<=l&&y>=r) { t[rt].cnt+=val; pushdown(rt,l,r); return; } int m=(l+r)>>1; if(x<=m) update(x,y,l,m,rt<<1,val); if(y>m) update(x,y,m+1,r,rt<<1|1,val); pushdown(rt,l,r); } int main() { int n,x1,x2,x3,x4,y1,y2,y3,y4; while(~scanf("%d",&n)&&n) { mem(t,0); int tot=0; for(int i=0; i<n; i++) { scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x4,&y4,&x2,&y2,&x3,&y3); pos[tot]=x1; e[tot++]=edge(x1,x4,y1,1); pos[tot]=x4; e[tot++]=edge(x1,x4,y2,-1); pos[tot]=x1; e[tot++]=edge(x1,x4,y3,1); pos[tot]=x4; e[tot++]=edge(x1,x4,y4,-1); pos[tot]=x1; e[tot++]=edge(x1,x2,y2,1); pos[tot]=x2; e[tot++]=edge(x1,x2,y3,-1); pos[tot]=x3; e[tot++]=edge(x3,x4,y2,1); pos[tot]=x4; e[tot++]=edge(x3,x4,y3,-1); } sort(pos,pos+tot); sort(e,e+tot); int m=unique(pos,pos+tot)-pos; ll ans=0; for(int i=0; i<tot; i++) { int x=lower_bound(pos,pos+m,e[i].l)-pos; int y=lower_bound(pos,pos+m,e[i].r)-pos-1; update(x,y,0,m,1,e[i].f); ans+=(e[i+1].h-e[i].h)*(ll)t[1].s; } printf("%lld\n",ans); } return 0; }

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6338    Accepted Submission(s): 1523 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
转载请注明原文地址: https://www.6miu.com/read-16872.html

最新回复(0)