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

来源:互联网 发布:昆明市零售业数据 编辑:程序博客网 时间:2024/06/04 20:10

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
西天取经~

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 发票货物名称多怎么办 发票上少打一个字怎么办 发票名称带星号怎么办 小贷太多还不上怎么办 生日当天买保险怎么办 我挪用公司货款怎么办 车辆改名字保险怎么办 工伤报案周六日怎么办 五菱宏光s1门下沉怎么办 新手机版本更新怎么办 戴尔电脑开机黑屏怎么办 淘宝退货不发货怎么办 铝被酸腐蚀怎么办 新娘头饰氧化了怎么办 合金饰品变黑了怎么办 麻醉机fico2升高怎么办 快递被恶意投诉怎么办 顺风快递收件人拒收怎么办 手机联系人没了怎么办 收件人号码错了怎么办 收件人写错了怎么办 快递联系不到收件人怎么办 小孩烫伤有水泡怎么办 被烫伤的疤痕怎么办 微店别人下单后怎么办 月经超了七天怎么办 收件人不在指定地址怎么办 2017款宝来熄火后异响怎么办 我身高一八米怎么办呀 规格型号错了发票怎么办 合同签错了怎么办 柜子背板起泡怎么办啊 显卡红灯不亮怎么办 小米设置全英文怎么办 小米手机变英文怎么办 阿迪贝壳头变黄怎么办 网友问我名字怎么办 护照姓名拼写错误怎么办 cfa报名时填错名字怎么办 机票拼音错了怎么办 evus忘填了怎么办