HDU - 1255 O - 覆盖的面积 (扫描线)

4 篇文章 0 订阅
2 篇文章 0 订阅

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

ps:之前自己看的一个模板,覆盖一次的是挺简单的,但是求两次以上就要改,索性贴个别人的比较方便的代码。

模板:

#include<iostream>  
#include<string>  
#include<cstdio>  
#include<cstring>  
#include<queue>  
#include<map>  
#include<cmath>  
#include<stack>  
#include<set>  
#include<vector>  
#include<algorithm>  
#define LL long long  
#define inf 1<<30  
using namespace std;  
const int N=2005;  
int n;  
double y[N];  
struct LINE  
{  
    double x;  
    double y_down,y_up;  
    int flag;  
}line[N];  
struct node  
{  
    double l,r;  
    double x;  
    int cover;  
    bool flag;  
}node[N<<2];  
bool cmp(LINE a,LINE b)  
{  
    return a.x<b.x;  
}  
void build(int rt,int l,int r)  
{  
    node[rt].l=y[l];  
    node[rt].r=y[r];  
    node[rt].x=-1;  
    node[rt].flag=false;  
    node[rt].cover=0;  
    if(l+1==r){  
        node[rt].flag=true;  
        return;  
    }  
    int mid=(l+r)>>1;  
    build(rt<<1,l,mid);  
    build(rt<<1|1,mid,r);  
}  
double Insert_query(int rt,double x,double l,double r,int flag)  
{  
    if(l>=node[rt].r||r<=node[rt].l) return 0;  
    if(node[rt].flag){  
        if(node[rt].cover>1){  
            double pre=node[rt].x;  
            double ans=(x-pre)*(node[rt].r-node[rt].l);  
            node[rt].x=x;  
            node[rt].cover+=flag;  
            return ans;  
        }else{  
            node[rt].x=x;  
            node[rt].cover+=flag;  
            return 0;  
        }  
    }  
    double ans1,ans2;  
    ans1=Insert_query(rt<<1,x,l,r,flag);  
    ans2=Insert_query(rt<<1|1,x,l,r,flag);  
    return ans1+ans2;  
}  
int main()  
{  
    int t;  
    double x1,x2,y1,y2;  
    scanf("%d",&t);  
    while(t--){  
        scanf("%d",&n);  
        int cnt=-1;  
        for(int i=0;i<n;i++){  
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);  
            y[++cnt]=y1;  
            line[cnt].x=x1;  
            line[cnt].y_down=y1;  
            line[cnt].y_up=y2;  
            line[cnt].flag=1;  
            y[++cnt]=y2;  
            line[cnt].x=x2;  
            line[cnt].y_down=y1;  
            line[cnt].y_up=y2;  
            line[cnt].flag=-1;  
        }  
        sort(y,y+cnt+1);  
        sort(line,line+cnt+1,cmp);  
        build(1,0,cnt);  
        double area=0;  
        for(int i=0;i<=cnt;i++){  
            area+=Insert_query(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag);  
        }  
        printf("%.2lf\n",area);  
    }  
    return 0;  
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值