[题解]hdu1828 Picture

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

Solution

矩形周长并。扫描线+线段树或者扫描线+暴力。我们可以横竖各扫一遍求出平行于x轴、y轴的周长,也可以只扫一遍,维护一下垂直于扫描线的周长的段数即可。垂直于扫描线的周长的段数等于被覆盖的区域个数*2。

代码:(扫描线+线段树)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=80010;
const double eps=1e-10;
struct Seg{
    double l,r,y;
    int f;
    bool operator<(Seg b)const{
        if(fabs(y-b.y)<=eps)return f<b.f;
        return y<b.y;
    }
}q[maxn];
int n,cnt,tot,tot2,mp[maxn];
double ans,a[maxn],b[maxn],sum[maxn];
int num[maxn],tag[maxn];
bool L[maxn],R[maxn];

int lower_bound(double *p,int l,int r,double val){
    int pos=r+1,mid;
    while(l<=r){
        mid=(l+r)>>1;
        if(val-p[mid]>eps)l=mid+1;
        else r=mid-1,pos=mid;
    }
    return pos;
}
void update(int x,int l,int r){
    sum[x]=(!tag[x]&&l!=r)*(sum[x<<1]+sum[x<<1|1])+(tag[x]>0)*(b[r+1]-b[l]);
    if(tag[x]){
        L[x]=R[x]=true;
        num[x]=1;
    }
    else if(l==r)L[x]=R[x]=num[x]=0;
    else{
        L[x]=L[x<<1];R[x]=R[x<<1|1];
        num[x]=num[x<<1]+num[x<<1|1]-(R[x<<1]&&L[x<<1|1]);
    }
}
void Modify(int x,int l,int r,int ql,int qr,int val){
    if(l>qr||r<ql)return;
    if(l>=ql&&r<=qr){
        tag[x]+=val;
        update(x,l,r);
        return;
    }
    int mid=(l+r)>>1;
    Modify(x<<1,l,mid,ql,qr,val);
    Modify(x<<1|1,mid+1,r,ql,qr,val);
    update(x,l,r);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        memset(mp,ans=cnt=tot=tot2=0,sizeof mp);
        memset(L,0,sizeof L);memset(R,0,sizeof R);
        memset(num,0,sizeof num);memset(sum,0,sizeof sum);
        memset(tag,0,sizeof tag);
        for(int i=1;i<=n;i++){
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[++tot]=x1;a[++tot]=x2;
            q[++cnt].l=x1;q[cnt].r=x2;
            q[cnt].y=y1;q[cnt].f=0;
            q[++cnt].l=x1;q[cnt].r=x2;
            q[cnt].y=y2;q[cnt].f=1;
        }
        sort(a+1,a+tot+1);
        sort(q+1,q+cnt+1);
        for(int i=1,j;i<=tot;i=j){
            b[++tot2]=a[i];j=i;
            while(fabs(a[j]-a[i])<=eps&&j<=tot)j++;
        }
        for(int i=1;i<=cnt;i++){
            q[i].l=lower_bound(b,1,tot2,q[i].l);
            q[i].r=lower_bound(b,1,tot2,q[i].r);
        }
        double lasty=0,lastans=0;
        for(int i=1;i<=cnt;i++){
            double l=0;
            if(i>1)ans+=2*num[1]*(q[i].y-lasty);
            if(!q[i].f)Modify(1,1,cnt-1,q[i].l,q[i].r-1,1);
            else Modify(1,1,cnt-1,q[i].l,q[i].r-1,-1);
            ans+=fabs(sum[1]-lastans);
            lasty=q[i].y;lastans=sum[1];
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值