覆盖的面积 HDU - 1255 扫描线求覆盖多次的面积

题意:

给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
这里写图片描述

输入:

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

输出:

对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
这里给出大神的解释,很容易懂:HUD-1255 O_O
主要是要注意cover = 1时的情况。刚开始我把cover的情况也直接算作 node[rt<<1].len + node[rt<<1|1].len了;但是这样其实会少算面积,因为cover = 1时,这部分面积被覆盖一次,但是这个节点的儿子的cover并没有加(线段树的特性),所以如果讲cover = 1当作cover = 0时的情况来看的话,这样的话实际上这部分的 cover =1 相当于没有加上,因为这部分的cover+1后就return了,对子节点并没有影响,只是让cover加了1。所以,如果这时候没对cover = 1的情况进行特判的话,相当与这部分就白覆盖了!~
代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2e3+9;
double y[MAX_N];
struct Line
{
    double x,y1,y2; // y1 < y2
    int flag;
}line[MAX_N<<2];
struct Node
{
    int l,r,cover;
    double lf,rf,len1,len2;
}node[MAX_N<<2];
bool cmp(const Line & A,const Line & B)
{
    return A.x < B.x;
}
void build(int rt,int l,int r)
{
    node[rt].l = l; node[rt].r = r;
    node[rt].lf = y[l]; node[rt].rf = y[r];
    node[rt].len1 = node[rt].len2 = node[rt].cover = 0;
    if(l+1 == r) return ;
    int mid = (l+r) >> 1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid,r);
}
void push_up(int rt)
{
    /*覆盖一次的长度处理*/
    if(node[rt].cover)
    {
        node[rt].len1 = node[rt].rf - node[rt].lf;
    }
    else if(node[rt].l+1==node[rt].r)
    {
        node[rt].len1 = 0;
    }
    else
    {
        node[rt].len1 = node[rt<<1].len1 + node[rt<<1|1].len1;
    }
    /*覆盖两次的长度处理*/
    if(node[rt].cover > 1)
    {
        node[rt].len2 = node[rt].rf - node[rt].lf;
        return;
    }
    else if(node[rt].l+1 == node[rt].r)
    {
        node[rt].len2 = 0;
    }
    else if(node[rt].cover == 1)
    {
         node[rt].len2 = node[rt<<1].len1 + node[rt<<1|1].len1;
    }
    else
    {
        node[rt].len2 = node[rt<<1].len2 + node[rt<<1|1].len2;
    }
}

void updata(int rt,Line t)
{
    if(t.y1 == node[rt].lf && t.y2 == node[rt].rf)
    {
        node[rt].cover += t.flag;
        push_up(rt);
        return;
    }
    if(t.y1 >= node[rt<<1|1].lf)
    {
        updata(rt<<1|1 , t);
    }
    else if(t.y2 <= node[rt<<1].rf)
    {
        updata(rt<<1 , t);
    }
    else
    {
        Line temp = t;
        temp.y2 = node[rt<<1].rf;
        updata(rt<<1 , temp);
        temp = t;
        temp.y1 = node[rt<<1|1].lf;
        updata(rt<<1|1 , temp);
    }
    push_up(rt);
}

int main()
{
    int N,M,T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&N);
        int cnt = 0;
        for(int i=0;i<N;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[cnt].x = x1;
            line[cnt].y1 = y1;
            line[cnt].y2 = y2;
            line[cnt].flag = 1;
            y[cnt++] = y1;
            line[cnt].x = x2;
            line[cnt].y1 = y1;
            line[cnt].y2 = y2;
            line[cnt].flag = -1;
            y[cnt++] = y2;
        }
        sort(line,line+cnt,cmp);
        sort(y,y+cnt);
        build(1,0,cnt-1);
        updata(1,line[0]);
        double ans = 0;
        for(int i=1;i<cnt;i++)
        {
            ans += node[1].len2*(line[i].x - line[i-1].x);
            updata(1,line[i]);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值