HDU - 1255 覆盖的面积(扫描线+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255点击打开链接

覆盖的面积

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


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
 


扫描线模版题 才去左右扫 为了训练一下离散化就自己加了

每个节点里记录离散前后的两个值 方便理解

#include <bits/stdc++.h>
using namespace std;
#define maxn 2005
struct Line
{
    double x;
    double yleft,yright;
    int flag;
};
struct xjy
{
    int left,right;
    double yleft,yright;
    int cnt;
    double once;
    double twice;
};
xjy tree[maxn<<2];
Line line[maxn];
double Hash[maxn];
bool cmp(Line a,Line b)
{
    return a.x<b.x;
}
void build(int i,int left,int right)
{
    tree[i].left=left;
    tree[i].right=right;
    tree[i].once=0;
    tree[i].twice=0;
    tree[i].yleft=Hash[left];
    tree[i].yright=Hash[right];
    if(left+1==right)
        return ;
    int mid=(left+right)>>1;
    build(i<<1,left,mid);
    build(i<<1|1,mid,right);
}
void check(int i)
{
    if(tree[i].cnt>=2)
    {
        tree[i].once=tree[i].yright-tree[i].yleft;
        tree[i].twice=tree[i].yright-tree[i].yleft;
        return ;
    }
    else if(tree[i].cnt==1)
    {
        tree[i].once=tree[i].yright-tree[i].yleft;
        if(tree[i].left+1==tree[i].right)
            tree[i].twice=0;
        else
            tree[i].twice=tree[i<<1].once+tree[i<<1|1].once;
    }
    else
    {
        if(tree[i].left+1==tree[i].right)
            tree[i].once=tree[i].twice=0;
        else
            {
                tree[i].once=tree[i<<1].once+tree[i<<1|1].once;
                tree[i].twice=tree[i<<1].twice+tree[i<<1|1].twice;
            }
    }
}

void update(int i,Line a)
{
    if(a.yleft==tree[i].yleft&&tree[i].yright==a.yright)
    {
        tree[i].cnt+=a.flag;
        check(i);
        //cout << tree[i].left << tree[i].right << endl;
        return;
    }
    if(a.yright<=tree[i<<1].yright)
        update(i<<1,a);
    else if(a.yleft>=tree[i<<1|1].yleft)
        update(i<<1|1,a);
    else
    {
        Line temp=a;
        temp.yright=tree[i<<1].yright;
        update(i<<1,temp);
        temp=a;
        temp.yleft=tree[i<<1|1].yleft;
        update(i<<1|1,temp);
    }
    check(i);
    //cout << tree[i].left << tree[i].right <<endl;
}

int main()
{
    int t;
    scanf("%d",&t);
    for(int step=1;step<=t;step++)
    {
        int n;int cnt=1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            double x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[cnt].yleft=y1;
            line[cnt].yright=y2;
            line[cnt].x=x1;
            line[cnt].flag=1;
            Hash[cnt++]=y1;

            line[cnt].yleft=y1;
            line[cnt].yright=y2;
            line[cnt].x=x2;
            line[cnt].flag=-1;
            Hash[cnt++]=y2;
        }
        sort(line+1,line+cnt,cmp);
        sort(Hash+1,Hash+cnt);
        build(1,1,cnt-1);
        double ans=0;
        update(1,line[1]);
        for(int i=2;i<cnt;i++)
        {
            ans+=tree[1].twice*(line[i].x-line[i-1].x);
            update(1,line[i]);
        }
        printf("%.2lf\n",ans);
    }
}












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值