您的位置:首页 > 其它

hdu 1828(poj 1177)Picture(线段树+扫描线)(轮廓线)

2017-06-14 12:26 459 查看

Picture

Problem 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.

Please process to the end of file.

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 25

15 -4 24 14

0 -6 16 4

2 15 10 22

30 10 36 20

34 0 40 16

Sample Output

228

ps:经典的周长并,仍旧是用扫描线

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn=10000+10;

struct Node
{
int le,ri;
int len;//表示底边的总长度
int cnt;//表示下底边比上底边多的个数
int numseg;//表示没有被覆盖的竖边对的个数
bool lcover,rcover;//表示左端点或者右端点是否被覆盖,用于正确计算竖边对的个数
int mid()
{
return (le+ri)>>1;
}
} tree[maxn<<2];
struct Line
{
int le,ri,h,f;
Line() {}
Line(int x1,int x2,int y,int flag)
{
le=x1,ri=x2,h=y,f=flag;
}
bool operator < (const Line&A)const
{
if(h==A.h)//注意这里,这样能够使得重和的上边和下边正确计算
return f>A.f;
return h<A.h;
}
} line[maxn];
int pos[maxn];

void Build(int rt,int le,int ri)
{
tree[rt].le=le,tree[rt].ri=ri;
tree[rt].cnt=tree[rt].len=tree[rt].numseg=0;
tree[rt].lcover=tree[rt].rcover=false;
if(le==ri)
return ;
int mid=tree[rt].mid();
Build(rt<<1,le,mid);
Build(rt<<1|1,mid+1,ri);
}

void Upfather(int rt)
{
if(tree[rt].cnt)//
{
tree[rt].len=pos[tree[rt].ri+1]-pos[tree[rt].le];
tree[rt].numseg=1;
tree[rt].lcover=tree[rt].rcover=true;
}
else if(tree[rt].le==tree[rt].ri)//叶子节点
{
tree[rt].len=tree[rt].numseg=0;
tree[rt].lcover=tree[rt].rcover=false;
}
else//由孩子信息得
{
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
tree[rt].numseg=tree[rt<<1].numseg+tree[rt<<1|1].numseg;
tree[rt].lcover=tree[rt<<1].lcover;
tree[rt].rcover=tree[rt<<1|1].rcover;
if(tree[rt<<1].rcover&&tree[rt<<1|1].lcover)//两个矩形相交(底边区域有重合)则减少竖边对的个数
--tree[rt].numseg;
}
}

void Update(int rt,int left,int right,int flag)
{
if(left<=tree[rt].le&&tree[rt].ri<=right)
{
tree[rt].cnt+=flag;
Upfather(rt);
return ;
}
int mid=tree[rt].mid();
if(left<=mid)
Update(rt<<1,left,right,flag);
if(right>mid)
Update(rt<<1|1,left,right,flag);
Upfather(rt);
}

int main()
{
int n;
int x1,y1,x2,y2;
while(~scanf("%d",&n))
{
int t=0;
for(int i=0; i<n; ++i)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[t]=Line(x1,x2,y1,1);
pos[t++]=x1;
line[t]=Line(x1,x2,y2,-1);
pos[t++]=x2;
}
sort(pos,pos+t);
sort(line,line+t);
int m=unique(pos,pos+t)-pos;//去重
Build(1,0,m-1);
int ans=0,last=0;
int left,right;
for(int i=0; i<t; ++i)
{
if(line[i].le<line[i].ri)
{
left=lower_bound(pos,pos+m,line[i].le)-pos;
right=lower_bound(pos,pos+m,line[i].ri)-pos-1;
Update(1,left,right,line[i].f);
}
ans+=tree[1].numseg*2*(line[i+1].h-line[i].h);//竖边增加的总长
ans+=abs(tree[1].len-last);//底边增加的总长
last=tree[1].len;
}
printf("%d\n",ans);
}
return 0;
}


参考博客:

Stephen

Titanium

西天取经~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: