【IOI1999】HDU1828 Picture

148 篇文章 0 订阅
36 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值