joj 2735: picture 求矩形并的周长

本文介绍了一个计算多个矩形粘贴在墙上的边界周长的问题,通过使用线段树和扫描线算法,有效地解决了计算任意覆盖情况下的总边界长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A number, N (1 <= N < 5000), 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.
Figure 1 shows an example with seven rectangles:
Figure 1. A set of seven rectangles
The corresponding boundary is the whole set of line segments drawn in Figure 2:
Figure 2. The boundary of the set of rectangles
The vertices of all rectangles have integer coordinates. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result fits in a 32-bit signed representation.

Input

MULTI TEST CASE!! Line 1: N, the number of rectangles pasted on the wall.
Lines 2..N+1 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.

Output

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

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ABS(x) ((x)>=0?(x):-(x))
const int MAXN=5500;
struct TSegNode
{
    int L,R,Lch,Rch,count,len;
    TSegNode(int x,int y):L(x),R(y),Lch(-1),Rch(-1),count(0),len(0){}
    TSegNode(){TSegNode(-1,-1);}
};
struct Tevent
{
    int L,R,x;bool style;
    friend const bool operator <(Tevent a,Tevent b)
    {
        if(a.x!=b.x) return a.x<b.x;
        return (a.style&& !b.style);
    }
};
int n,lx[MAXN],ly[MAXN],ux[MAXN],uy[MAXN],total,nevent,res;
TSegNode node[MAXN*4];
Tevent event[MAXN*4];
void CreatTree(int r)
{
    if(node[r].R-node[r].L>1)
    {
        int mid=(node[r].R+node[r].L)>>1;
        node[total]=TSegNode(node[r].L,mid);
        node[r].Lch=total;CreatTree(total++);
        node[total]=TSegNode(mid,node[r].R);
        node[r].Rch=total;CreatTree(total++);
    }
}
void update(int r,int L,int R,int v)
{
    if(L>=node[r].R||R<=node[r].L) return ;
    if(L<=node[r].L&&R>=node[r].R)
    {
        node[r].count+=v;
        if(v>0&&v==node[r].count) node[r].len=node[r].R-node[r].L;
        if(v<0&&node[r].count==0) if(node[r].Lch<0) node[r].len=0;
        else node[r].len=node[node[r].Lch].len+node[node[r].Rch].len;
    }
    else
    {
        update(node[r].Lch,L,R,v);update(node[r].Rch,L,R,v);
        if(node[r].count==0)
            node[r].len=node[node[r].Lch].len+node[node[r].Rch].len;
    }
}
void process()
{
    int nlist,list[MAXN*2],last,i,now;
    nevent=0,nlist=0;
    for(i=0;i<n;i++)
    {
        event[nevent].x=lx[i];event[nevent].L=ly[i];
        event[nevent].R=uy[i];event[nevent++].style=true;
        event[nevent].x=ux[i];event[nevent].L=ly[i];
        event[nevent].R=uy[i];event[nevent++].style=false;
        list[nlist++]=ly[i];list[nlist++]=uy[i];
    }
    sort(event,event+nevent);
    sort(list,list+nlist);
    nlist=unique(list,list+nlist)-list;
    node[total=0,total++]=TSegNode(0,nlist-1);
    CreatTree(0);
    for(i=0;i<total;i++)
    {
        node[i].L=list[node[i].L];node[i].R=list[node[i].R];
    }
    last=i=0;
    while(i<nevent)
    {
        now=event[i].x;
        while(i<nevent&&event[i].x==now&&event[i].style)
        {
            update(0,event[i].L,event[i].R,1);++i;
        }
        res+=ABS(node[0].len-last);last=node[0].len;
        while(i<nevent&&event[i].x==now)
        {
            update(0,event[i].L,event[i].R,-1);++i;
        }
        res+=ABS(node[0].len-last);last=node[0].len;
    }
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++) scanf("%d%d%d%d",&lx[i],&ly[i],&ux[i],&uy[i]);
        res=0;process();
        for(int i=0;i<n;i++)
        {
            swap(lx[i],ly[i]);swap(ux[i],uy[i]);
        }
        process();
        printf("%d\n",res);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值