POJ1151 ,hdu1542,codevs3304(离散化,线段树,扫描线)

题目链接:http://codevs.cn/problem/3044/

题目描述 Description
输入n个矩形,求他们总共占地面积(也就是求一下面积的并)
输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组)
每组数据第一行一个数n,表示矩形个数(n<=100)
接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <=
100000),表示矩形的左下角坐标和右上角坐标
输出描述 Output Description
每组数据输出一行表示答案

样例输入 Sample Input
2 10 10 20 20 15 15 25 25.5 0
样例输出 Sample Output
180.00

这个题感觉用到的知识点挺多,综合性挺强,当然还有其他不同的解法,这里的仅供参考

参考博客:https://blog.csdn.net/xianpingping/article/details/83032798
https://blog.csdn.net/riba2534/article/details/76851233
https://www.jianshu.com/p/d70f6d346913

分析:
什么是扫描线

把矩形用两条扫描线代替,也就是可以看做上下两条边。这条边就用线段树维护
为什么要离散化坐标?
因为x,y都是小数,线段树无法维护小数区间,或者说无法维护太大的区间
怎么离散化?
简单来说比如 1.5,2.4,9.87,12.3这四个数字,我离散化一下,使他们分别对应1,2,3,4(也就是映射)。
那么我对[1,2]区间的改变,也就是对应的[1.5,2.4]区间的修改,使得线段树通过映射来维护小数区间
实现
从下往上遍历扫描线,然后区间更新,计算每两条扫描线之间的矩形面积即可

代码:
#include<bits/stdc++.h>

using namespace std;

#define lson i<<1,l,m
#define rson i<<1|1,m+1,r

const int maxn=222;

double x[maxn];
struct node
{
    double l,r,h;//左右坐标,高度
    int d;//标记上位边还是下位边
    node() {}
    node(double l,double r,double h,int d):l(l),r(r),h(h),d(d) {}
    bool operator < (const node &a)const
    {
        return h<a.h;
    }
} line[maxn];

int cnt[maxn<<2];
double sum[maxn<<2];

void pushup(int i,int l,int r)
{
   if(cnt[i])
   {
       sum[i]=x[r+1]-x[l];
   }
   else
   {
       sum[i]=sum[i<<1]+sum[i<<1|1];
   }
}



void update(int ql,int qr,int v,int i,int l,int r)
{
    if(ql<=l && qr>=r)
    {
       cnt[i]+=v;
       pushup(i,l,r);
       return ;
    }

    int m=(l+r)>>1;
    if(ql<=m)update(ql,qr,v,lson);
    if(qr>m)update(ql,qr,v,rson);
    pushup(i,l,r);
}


int main()
{
    int q;
    int kase=0;
    while(cin>>q&&q)
    {
        memset(cnt,0,sizeof(cnt));//相当于build
        memset(sum,0,sizeof(sum));//相当于build
        int n=0,m=0;
        for(int i=1; i<=q; i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            x[++n]=x1;
            x[++n]=x2;
            line[++m]=node(x1,x2,y1,1);
            line[++m]=node(x1,x2,y2,-1);
        }

        sort(x+1,x+1+n);
        sort(line+1,line+1+m);
        int k=1;
        /* for(int i=2;i<=n;i++)//去重
         {
             if(x[i]!=x[i-1])x[++k]=x[i];
         }*/
        k=unique(x+1,x+n+1)-x-1;//直接用STL中的unique函数。
        double ans=0.0;
        for(int i=1; i<m; i++)
        {

            int l=lower_bound(x+1,x+k+1,line[i].l)-x;
            int r=lower_bound(x+1,x+k+1,line[i].r)-x;
            r--;

            if(l<=r)update(l,r,line[i].d,1,1,k-1);
            printf("l=%d r=%d  sum=%f\n",l,r,sum[1]);
            ans+=sum[1]*(line[i+1].h-line[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++kase,ans);
    }
}
/*


1
10 10 20 20


*/
/*
1
10 10 20 20

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值