Atlantis

主要这篇文章想说一些自己遇到的困惑

1. 线段树维护的内容是什么,以y轴建立扫描线为例 ,我们知道当遇到两条边的时候间距很好求出,然而高度却不好求出,并且随着出边与入边的改变,在y轴上被划分的小矩形块的一直在变化,因此,需要维护的就是这个长度。

2.    为什么这段代码当中 r要 -1 ,l 不用?  

    l = lower_bound(ally + 1,ally + 1 + cnt,e[i].a) - ally;
    r = lower_bound(ally + 1,ally + 1 + cnt,e[i].b) - ally - 1;

试想一个问题,五个区间的间隔是多少? 显然是4,我们储存在线段树当中的 l r 并不是端点,而是一个个的区间 !当我们进行修改操作的时候,l代表的其实是从第几个区间开始,r代表第几个区间之前结束,因此 l = 1 ,r = 3  时,我们其实只经过了第 1 和第2个区间。

3.为什么下面代码是 r + 1 呢? 这个时候其实我们使用的是区间左右端点的y值,试想,第三个区间的终点,是不是对应的ally[4]呢(ally记录从小到大,离散后的y的值)?,

 if(t[p].cnt) t[p].length = ally[t[p].r + 1] - ally[t[p].l];

4. 为什么cnt = 0 的时候有 t[p].length = t[p * 2].length + t[p * 2 + 1].length;呢?

我们要时刻维护线段树的正确性,当更新cnt值的时候,其子区间也得全部被更新为cnt = 0,因此,这样子写,先递归到最底层,然后逐层递归上来,每个子区间都被更新了。

下附代码:

#include<bits/stdc++.h>
using namespace std;

int n,m,cnt;
typedef long long ll;
const int N = 1001;
double ally[N];
struct node
{
    double x,a,b;
    int k;
    node(){}
    node(double a ,double b ,double c ,int d ):x(a),a(b),b(c),k(d) {}
   
}e[N];
 bool cmp(node a ,node b)
    {
        return a.x < b.x;
    }


struct SegmentTree
{
    int l,r;
    int cnt;
    double length;
} t[N * 4];


void build(int p , int l ,int r)
{
    t[p].l = l , t[p].r = r;
    t[p].length = t[p].cnt = 0;
     if(l==r){
      //  t[p].length = t[p].cnt = 0;
         t[p].l = l , t[p].r = r;
        return;
    }
    int mid = (l + r) / 2;
    build(p * 2 ,l,mid);
    build(p * 2 + 1,mid + 1,r);
}


void change(int p, int l,int r, int  v )
{
    if(l <= t[p].l && r >= t[p].r)
    { 
        t[p].cnt += v;
        if(t[p].cnt) t[p].length = ally[t[p].r + 1] - ally[t[p].l];
   
        else t[p].length = t[p * 2].length + t[p * 2 + 1].length;
        return ;    //递归最底层
    }
    
    int mid =(t[p].l + t[p].r) / 2;
    if(l <= mid) change(p*2, l,r, v);
    if(r > mid) change(p * 2 + 1, l,r, v);
    if(t[p].cnt) t[p].length = ally[t[p].r + 1] - ally[t[p].l];
    else t[p].length = t[p * 2].length + t[p * 2 + 1].length;
}

int main(){
    int k = 0;
     while(~scanf("%d",&n) && n)
    {
        printf("Test case #%d\n",++k);
        cnt = 0;
        double x1,y1,x2,y2;
        int num = 0;
        int numy = 0;
        for(int i = 1 ; i <= n ;i++)
        {
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            e[++num] = node(x1,y1,y2,1);
            e[++num] = node(x2,y1,y2,-1);
            ally[++numy] = y1;
            ally[++numy] = y2;
        }
         
        
        sort(e + 1,e + 1 + num,cmp);
        sort(ally + 1 ,ally + 1 + numy);   
        cnt = unique(ally + 1 ,ally + 1 + numy) - ally - 1;
        //for(int i = 1 ;i <= cnt ;i ++) cout <<ally[i] << endl;
        //cout << cnt;
        build(1,1,cnt - 1);
        double ans = 0;
        int l,r;
         l = lower_bound(ally + 1,ally + 1 + cnt,e[1].a) - ally;
         r = lower_bound(ally + 1,ally + 1 + cnt,e[1].b) - ally - 1;
         change(1, l, r, e[1].k);
        //  cout << t[1].length <<endl;
         // printf("%lf %lf -> %d %d\n",ally[l],ally[r],l,r);
         for(int i = 2 ; i <= num; i++)
         {
             ans +=(e[i].x -e[i - 1].x)*(t[1].length);
            // cout << t[1].length <<endl;
             l = lower_bound(ally + 1,ally + 1 + cnt,e[i].a) - ally;
             r = lower_bound(ally + 1,ally + 1 + cnt,e[i].b) - ally - 1;
            // printf("%lf %lf -> %d %d\n",ally[l],ally[r],l,r);
             change(1, l, r, e[i].k);
         }
         printf("Total explored area: %.2f\n\n",ans);
    }
    
    
    
}

 

 对于扫描线的介绍,这篇写的不错戳这里 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值