杭电1255 覆盖的面积(扫描线)

覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3911    Accepted Submission(s): 1931


Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.


 

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
 

Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 

Sample Input
  
  
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
 

Sample Output
  
  
7.63 0.00
 

Author
Ignatius.L & weigang Lee
/*
pushup的时候忘了r-l==1时等于0 了。。。加油!!
Time:2015-1-21 19:11
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid,r,rt<<1|1
const int MAX=2000+10;
struct Tree{
    double lf,rf,len[3];
    int covers;
}tree[MAX<<2];
struct Line{
    double x,y1,y2;
    int Left;
}line[MAX];
double yNode[MAX];
void Build(int l,int r,int rt){
    tree[rt].covers=0;
    tree[rt].len[1]=tree[rt].len[2]=0;
    tree[rt].len[0]=yNode[r]-yNode[l];
    tree[rt].lf=yNode[l];
    tree[rt].rf=yNode[r];
    if(l+1==r)return;
    int mid=(l+r)>>1;
    Build(lson);
    Build(rson);
}
void pushUp(int l,int r,int rt){
    if(tree[rt].covers>1){
        tree[rt].len[2]=tree[rt].len[0];
        tree[rt].len[1]=0;
    }else if(tree[rt].covers==1){
        if(r-l==1){
            tree[rt].len[2]=0;
            tree[rt].len[1]=tree[rt].len[0];
        }else{
            tree[rt].len[2]=tree[rt<<1].len[2]+tree[rt<<1|1].len[2]
                +tree[rt<<1].len[1]+tree[rt<<1|1].len[1];

            tree[rt].len[1]=tree[rt].len[0]-tree[rt].len[2];
        }
    }else{
        if(l+1==r){
            tree[rt].len[2]=0;
            tree[rt].len[1]=0;
        }else{
            tree[rt].len[2]=tree[rt<<1].len[2]+tree[rt<<1|1].len[2];
            tree[rt].len[1]=tree[rt<<1].len[1]+tree[rt<<1|1].len[1];
        }
    }
}
void update(Line seg,int l,int r,int rt){
    if(seg.y1==tree[rt].lf&&seg.y2==tree[rt].rf){
        tree[rt].covers+=seg.Left;
        pushUp(l,r,rt);
        return;
    }
    int mid=(l+r)>>1;
    if(seg.y2<=tree[rt<<1].rf) update(seg,lson);
    else if(seg.y1>=tree[rt<<1|1].lf) update(seg,rson);
    else {
        Line temp=seg;
        temp.y2=tree[rt<<1].rf;
        update(temp,lson);
        temp=seg;
        temp.y1=tree[rt<<1|1].lf;
        update(temp,rson);
    }
    pushUp(l,r,rt);
}
bool cmp(Line a,Line b){
    return a.x<b.x;
}
int main(){
    int T,n;
    double x1,y1,x2,y2;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int k=1;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[k].y1=y1;
            line[k].y2=y2;
            line[k].x=x1;
            line[k].Left=1;
            yNode[k++]=y1;

            line[k].y1=y1;
            line[k].y2=y2;
            line[k].x=x2;
            line[k].Left=-1;
            yNode[k++]=y2;
        }
        sort(yNode+1,yNode+k);
        sort(line+1,line+k,cmp);
        Build(1,k,1);
        double ans=0;
        update(line[1],1,k,1);

        for(int i=2;i<k;i++){

            ans+=tree[1].len[2]*(line[i].x-line[i-1].x);
            update(line[i],1,k,1);
        }
        printf("%.2lf\n",ans);
    }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值