HDU1828-Picture(扫描线求周长)

Picture

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5736 Accepted Submission(s): 2726

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
题目:HDU1828
题意:给定n个矩形的左下点坐标和右上坐标,求矩形组成的新图形的周长。
思路:一道扫描线题,坐标有负数,先离散化坐标(不懂的戳)。
如果按照一般扫描线的做法,从下向上扫描一遍,会发现竖边的情况很难处理,索性先处理横边,再横向扫一遍处理竖边,这样我们就要离散化两套点,两套边。
接下来就是扫描线更新的问题。更新时,注意如下几点:
1.如果扫到一条边是外边(对答案有贡献的边),那么答案直接加上这条边贡献长度(有可能这条边只有一部分对答案有贡献)的两倍。因为你总可以把一个由小矩形拼接而成的不规则图形变成一个大矩形(内部镂空部分也可以),对每一条对答案有贡献的下边,都可以找到相同长度的上边。这样可以之考虑下边对答案的贡献。
2.每条边最多对答案贡献一次。
具体思路,见代码注释。
Tips:题目数据好像有问题,对于两条重在一起的边,会计算2次,类似日字形的两个矩形,上矩形的下边和下矩形的上边都会对答案有贡献。
AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int cont,numx,numy;
ll pos[2][maxn];//两套点
struct Tree
{
    ll ans,line;
    int vis,flag;//vis标记区间覆盖次数,flag标记区间是否对答案贡献
}tree[maxn];
struct Edge
{
    ll xl,xr,h;
    int flag;
    Edge(){};
    Edge(ll a,ll b,ll c,int d) : xl(a),xr(b),h(c),flag(d){}
    bool operator< (const Edge &a)const{
        return h<a.h;
    }
}edgex[maxn],edgey[maxn];//两套边
void pushup(int l,int r,int root,int c)
{
    if(tree[root].vis)tree[root].line=pos[c][r+1]-pos[c][l];
    else if(l==r)tree[root].line=0;
    else tree[root].line=tree[root*2].line+tree[root*2+1].line;//这三行是计算扫描线的长度
    if(tree[root].vis==1&&tree[root].flag==0)tree[root].ans=2*(tree[root].line-tree[root*2].line-tree[root*2+1].line),tree[root].flag=1;//vis==1时,表示这条边第一次被加进来,flag==0表示还没有计算这条边对答案的贡献
    else if(tree[root].vis==0&&tree[root].flag==1)tree[root].ans=0,tree[root].flag=0;//当vis==0时,表示原边的上边已经扫过,该节点所表示区间可以再次对答案贡献
    else if(tree[root].vis==0)tree[root].ans=tree[root*2].ans+tree[root*2+1].ans;//节点vis==0时,说明节点表示区间没有边覆盖,可以直接由子节点更新
    else tree[root].ans=0;
    tree[root*2].ans=tree[root*2+1].ans=0;//一条线的答案只贡献一次
}

void update(int nl,int nr,int add,int l,int r,int root,int c)//c表示取哪套边
{
    if(nl==l&&nr==r)
    {
        tree[root].vis+=add;
        pushup(nl,nr,root,c);
        return ;
    }
    int mid=(l+r)/2;
    if(nr<=mid)update(nl,nr,add,l,mid,root*2,c);
    else if(nl>mid)update(nl,nr,add,mid+1,r,root*2+1,c);
    else update(nl,mid,add,l,mid,root*2,c),update(mid+1,nr,add,mid+1,r,root*2+1,c);
    pushup(l,r,root,c);
}
int main()
{
    int cas=0,n;
    while(~scan(n))
    {
        ll ans=0;
        met(tree,0);
        cont=0;
        numx=1;
        numy=1;
        for(int i=0;i<n;i++)
        {
            ll x,y,z,m;
            scanf("%lld%lld%lld%lld",&x,&y,&z,&m);
            edgex[cont]=Edge(x,z,y,1);
            pos[0][cont]=x;
            edgey[cont]=Edge(y,m,x,1);
            pos[1][cont++]=y;
            edgex[cont]=Edge(x,z,m,-1);
            pos[0][cont]=z;
            edgey[cont]=Edge(y,m,z,-1);
            pos[1][cont++]=m;
        }
        sort(pos[0],pos[0]+cont);
        sort(edgex,edgex+cont);
        sort(pos[1],pos[1]+cont);
        sort(edgey,edgey+cont);
        for(int i=1;i<cont;i++)if(pos[0][i]!=pos[0][i-1])pos[0][numx++]=pos[0][i];
        for(int i=1;i<cont;i++)if(pos[1][i]!=pos[1][i-1])pos[1][numy++]=pos[1][i];
        for(int i=0;i<cont-1;i++)
        {
            unsigned int l=lower_bound(pos[0],pos[0]+numx,edgex[i].xl)-pos[0];
            unsigned int r=lower_bound(pos[0],pos[0]+numx,edgex[i].xr)-pos[0]-1;
            update(l,r,edgex[i].flag,0,numx-1,1,0);
            ans+=tree[1].ans;
        }
        met(tree,0);
        for(int i=0;i<cont-1;i++)
        {
            unsigned int l=lower_bound(pos[1],pos[1]+numy,edgey[i].xl)-pos[1];
            unsigned int r=lower_bound(pos[1],pos[1]+numy,edgey[i].xr)-pos[1]-1;
            update(l,r,edgey[i].flag,0,numy-1,1,1);
            ans+=tree[1].ans;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值