POJ 1177 Picture

来源:互联网 发布:重庆外包seo找谁好 编辑:程序博客网 时间:2024/05/19 06:49

Description

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 

The corresponding boundary is the whole set of line segments drawn in Figure 2. 

The vertices of all rectangles have integer coordinates. 

Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

Sample Input

7-15 0 5 10-5 8 20 2515 -4 24 140 -6 16 42 15 10 2230 10 36 2034 0 40 16

Sample Output

228

Source

IOI 1998
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

扫描线+线段树~

扫描线经典题目矩形周长并~

我们将每个矩形拆成两条竖着的边,左面的flag=1右面的flag=-1,表示加入和删除。然后用线段树维护目前的横向线段数以及纵向总长度,线段树a[k]包含l、r(左右边界,[l,mid],[mid,r]要包含mid),num(线段数量),len(覆盖长度),cover(区间是否被覆盖),ls、rs(左节点左和右节点右是否被覆盖)。

要注意的是这里l==r-1时要返回。

(因为读入时1.txt写成了1,txt检查了一晚上+一上午……)

(POJ挂掉了……程序没有测过~)


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,x1,x2,y1,y2,cnt,xx[10001],len,now,ans,las;struct node{int x,y1,y2,flag;}l[10001];struct node1{int l,r,num,len,cover;bool ls,rs;}a[10001<<2];bool operator < (node u,node v){return u.x==v.x ? u.flag>v.flag:u.x<v.x;}int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void build(int l,int r,int k){a[k].l=l;a[k].r=r;if(l==r-1) return;int mid=l+r>>1;build(l,mid,k<<1);build(mid,r,k<<1|1);}void update(int k){if(a[k].cover>0){a[k].num=a[k].ls=a[k].rs=1;a[k].len=xx[a[k].r]-xx[a[k].l];return;}if(a[k].l==a[k].r-1){a[k].num=a[k].len=a[k].ls=a[k].rs=a[k].cover=0;return;}a[k].ls=a[k<<1].ls;a[k].rs=a[k<<1|1].rs;a[k].len=a[k<<1].len+a[k<<1|1].len;a[k].num=a[k<<1].num+a[k<<1|1].num-(a[k<<1].rs&a[k<<1|1].ls);}void chan(int k,int z){int ll=a[k].l,r=a[k].r,mid=ll+r>>1;if(xx[ll]>=l[z].y1 && xx[r]<=l[z].y2){a[k].cover+=l[z].flag;update(k);return;}if(ll==r-1) return;if(xx[mid]>=l[z].y1) chan(k<<1,z);if(xx[mid]<l[z].y2) chan(k<<1|1,z);update(k);}int main(){n=read();for(int i=1;i<=n;i++){x1=read(),y1=read(),x2=read(),y2=read();xx[++cnt]=y1;l[cnt]=(node){x1,y1,y2,1};xx[++cnt]=y2;l[cnt]=(node){x2,y1,y2,-1};}sort(xx+1,xx+cnt+1);len=unique(xx+1,xx+cnt+1)-xx-1;sort(l+1,l+cnt+1);build(1,len,1);for(int i=1;i<=cnt;i++){chan(1,i);if(i>1) ans+=now*2*(l[i].x-l[i-1].x);ans+=abs(a[1].len-las);las=a[1].len;now=a[1].num;}printf("%d\n",ans);return 0;}


原创粉丝点击