hdu1542线段树+离散化+扫描线详解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542

题目大意:给出一些矩形的左下角和右上角的坐标 求最后总的覆盖面积

思路:

一:变量的解释。

struct Seg
{
	double h , l , r;
	int s;
	Seg(){}
	Seg(double a,double b,double c,int d) : l(a) , r(b) , h(c) , s(d) {}
	bool operator < (const Seg &cmp) const
	{
		return h < cmp.h;
	}
}ss[maxn];

s 表示一条线段 , 分别有线段的左右端点 l,r, 线段的高 h,   线段是矩形的上底边还是还是下底边 d:-1/1,

假设给出的矩形如上图,红色字表示相应边的高,

9条线段

经从小到大排序后的边的高度h:2,3,3,4,5,6,7,7,8,9

                    相应的d:1,1,1,-1,1,1,-1,-1,-1,-1

二:对中间r的+1和-1的解释

int r = Bin(ss[i].r , k , X) - 1;

if (cnt[rt])
	sum[rt] = X[r+1] - X[l];//需要理解  表示该区间整个线段长度可以作为底边
解释:假设现在有一个线段左端点是l=0,右端点是r=m-1
则我们去更新的时候,会算到sum[1]=hash[mid]-hash[left]+hash[right]-hash[mid+1]
这样的到的底边长sum是错误的,why?因为少算了mid~mid+1的距离,由于我们这利用了
离散化且区间表示线段,所以mid~mid+1之间是有长度的,比如hash[3]=1.2,hash[4]=5.6,mid=3
所以这里用r-1,r+1就很好理解了 
一开始主函数为什么要对区间的右边界r减一,因为在计算 线段长度是,他会将右边一个单位长度的距离多加进去,主函数减一,是为了统一计算

直接上AC代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 2222;
int cnt[maxn << 2];//记录某个区间的下底边个数
double sum[maxn << 2];//记录某个区间的下底边总长度
double X[maxn];//对x进行离散化,否则x为浮点数且很大无法进行线段树
//以横坐标作为线段(区间),对横坐标线段进行扫描
//扫描的作用是每次更新下底边总长度和下底边个数,增加新面积
struct Seg
{
    double h , l , r;
    int s;
    Seg(){}
    Seg(double a,double b,double c,int d) : l(a) , r(b) , h(c) , s(d) {}
    bool operator < (const Seg &cmp) const
    {
        return h < cmp.h;
    }
}ss[maxn];
void PushUp(int rt,int l,int r) {
    if (cnt[rt])
        sum[rt] = X[r+1] - X[l];//需要理解 //表示该区间整个线段长度可以作为底边
    else if(l == r)
        sum[rt] = 0;
    else
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if (L <= l && r <= R)//该区间是当前扫描线段的一部分,则该区间下底边总长以及上下底边个数差更新
    {
        cnt[rt] += c;//这个位置的边的条数  如果大于0时需要计入底边
        PushUp(rt , l , r);//更新底边长
        return ;
    }
    int m = (l + r) >> 1;
    if (L <= m)
        update(L , R , c , lson);
    if (m < R)
        update(L , R , c , rson);
    PushUp(rt , l , r);
}
int main()
{
    int n , cas = 1;
    while (~scanf("%d",&n) && n)
    {
        int m = 0;
        while (n --)
        {
            double a , b , c , d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            X[m] = a;
            ss[m++] = Seg(a , c , b , 1);
            X[m] = c;
            ss[m++] = Seg(a , c , d , -1);
        }
        sort(X , X + m);
        sort(ss , ss + m);
        int k=unique(X,X+m)-X;
        memset(cnt , 0 , sizeof(cnt));
        memset(sum , 0 , sizeof(sum));
        double ret = 0;
        for (int i = 0 ; i < m - 1 ; i ++)
        {
            int l=lower_bound(X,X+k,ss[i].l)-X;
            int r =lower_bound(X,X+k,ss[i].r)-X-1;
            if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1);//扫描线段时更新底边长度和底边相差个数
                ret += sum[1] * (ss[i+1].h - ss[i].h);//底乘以高
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++ , ret);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值