hdu1828 Picture 扫描线

来源:互联网 发布:义乌司法拍卖淘宝网站 编辑:程序博客网 时间:2024/05/21 06:57

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

做了好几天竟然发现是一个变量写错了。。。
让你秋的是图形的周长,你只需要上下扫描一遍左右扫描一遍就行了。
#include<cstdio>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int y[100000];int x[100000];struct node{    int l,r,flag;    int  yl,yr,s;} tree[44000];struct w{    int x,yl,yr;    int flag;} p[44000],tem,q[44000];int cmp(struct w a,struct w b){    return a.x<b.x;}void build(int left,int right,int root){    tree[root].l=left;    tree[root].r=right;    tree[root].s=tree[root].flag=0;    tree[root].yl=y[left];    tree[root].yr=y[right];    if(left+1==right)        return;    int mid=(left+right)>>1;    build(left,mid,root<<1);    build(mid,right,root<<1|1);}void build2(int left,int right,int root){    tree[root].l=left;    tree[root].r=right;    tree[root].s=tree[root].flag=0;    tree[root].yl=x[left];    tree[root].yr=x[right];    if(left+1==right)        return;    int mid=(left+right)>>1;    build2(left,mid,root<<1);    build2(mid,right,root<<1|1);}void len(int t){    if(tree[t].flag>0)    {        tree[t].s=tree[t].yr-tree[t].yl;        return ;    }    if(tree[t].l+1==tree[t].r)        tree[t].s=0;    else        tree[t].s=tree[t<<1].s+tree[t<<1|1].s;}void update(int t,w e){    if(e.yl==tree[t].yl&&e.yr==tree[t].yr)    {        tree[t].flag+=e.flag;        len(t);        return;    }    if(e.yr<=tree[t<<1].yr)        update(t<<1,e);    else if(e.yl>=tree[t<<1|1].yl)        update(t<<1|1,e);    else    {        w tmp=e;        tmp.yr=tree[t<<1].yr;        update(t<<1,tmp);        tmp=e;        tmp.yl=tree[t<<1|1].yl;        update(t<<1|1,tmp);    }     len(t);}int main(){    int t,cnt,cnt2;    int x1,x2,y1,y2;    while(~scanf("%d",&t)&&t)    {        cnt=1;        cnt2=1;        while(t--)        {            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);            q[cnt2].x=x1;            q[cnt2].yl=y1;            q[cnt2].yr=y2;            q[cnt2].flag=1;            x[cnt2++]=y1;            q[cnt2].x=x2;            q[cnt2].yl=y1;            q[cnt2].yr=y2;            q[cnt2].flag=-1;            x[cnt2++]=y2;            p[cnt].x=y1;            p[cnt].yl=x1;            p[cnt].yr=x2;            p[cnt].flag=1;            y[cnt++]=x1;            p[cnt].x=y2;            p[cnt].yl=x1;            p[cnt].yr=x2;            p[cnt].flag=-1;            y[cnt++]=x2;        }        sort(p+1,p+cnt,cmp);        sort(y+1,y+cnt);        build(1,cnt-1,1);        update(1,p[1]);        int  sum=tree[1].s;        int low=tree[1].s;        for(int i=2; i<cnt; i++)        {            update(1,p[i]);            sum+=abs(low-tree[1].s);            low=tree[1].s;        }        sort(q+1,q+cnt2,cmp);        sort(x+1,x+cnt2);        build2(1,cnt2-1,1);        update(1,q[1]);          sum+=tree[1].s;          low=tree[1].s;        for(int i=2; i<cnt2; i++)        {            update(1,q[i]);            sum+=abs(low-tree[1].s);             low=tree[1].s;        }        printf("%d\n",sum);    }}


0 0
原创粉丝点击