【IOI1999】HDU1828 Picture

来源:互联网 发布:鬼泣3 但丁觉醒 mac版 编辑:程序博客网 时间:2024/06/10 07:55

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.

和求面积并【poj1151 Atlantis】很类似。
分别沿x轴和y轴扫描,每次统计与当前轴垂直的轴上的长度,具体就是这一次的总长度-上一次的总长度。
poj的数据比较弱,hdu的数据强。注意考虑重边,要先加再减。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxm=10000;struct seg{    int x,y1,y2,k;    bool operator < (const seg &ss) const    {        return x<ss.x||(x==ss.x&&k>ss.k);    }}a[100010],b[100010];int abs(int x){    return x>0?x:-x;}int n,cov[1000010],tot,m,sum[1000010];void mark(int p,int L,int R,int l,int r,int k){    if (L==l&&R==r)    {        cov[p]+=k;        if (cov[p]) sum[p]=R-L+1;        else sum[p]=sum[p*2]+sum[p*2+1];        return;    }    int M=(L+R)/2;    if (r<=M) mark(p*2,L,M,l,r,k);    else    {        if (l>M) mark(p*2+1,M+1,R,l,r,k);        else        {            mark(p*2,L,M,l,M,k);            mark(p*2+1,M+1,R,M+1,r,k);        }    }    if (cov[p]) sum[p]=R-L+1;    else sum[p]=sum[p*2]+sum[p*2+1];}int main(){    int i,j,k,p,q,x,y,z,K=0,x1,x2,y1,y2,last,ans;    while (scanf("%d",&n)==1)    {    for (i=1;i<=n;i++)          {        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        x1+=maxm;        x2+=maxm;        y1+=maxm;        y2+=maxm;        a[2*i-1]=(seg){x1,y1,y2,1};        a[2*i]=(seg){x2,y1,y2,-1};        b[2*i-1]=(seg){y1,x1,x2,1};        b[i*2]=(seg){y2,x1,x2,-1};    }    ans=0;    sort(a+1,a+2*n+1);    last=0;    memset(sum,0,sizeof(sum));    memset(cov,0,sizeof(cov));    for (i=1;i<=2*n;i++)    {        mark(1,1,2*maxm,a[i].y1+1,a[i].y2,a[i].k);        ans+=abs(sum[1]-last);        last=sum[1];    }    sort(b+1,b+2*n+1);    last=0;    memset(sum,0,sizeof(sum));    memset(cov,0,sizeof(cov));    for (i=1;i<=2*n;i++)    {        mark(1,1,2*maxm,b[i].y1+1,b[i].y2,b[i].k);        ans+=abs(sum[1]-last);        last=sum[1];    }    printf("%d\n",ans);}}
0 0
原创粉丝点击