poj 1151 && hdu 1542 求矩形面积并(线段树)

线段树的做法大致如下:

       1输入每个矩形,标记左边1,右边0,并记录矩形的横坐标与纵坐标;

       2离散化处理。将矩形的横坐标和纵左边排序;

       3对纵坐标进行去重;

       4建立线段树,用纵坐标重后的个数来建立线段树;

       5扫面法,求矩形相交的面积。这里对矩形的左边和右边的处理是不一样的。做边进行插入操作,右边进行删除操作。因为是按纵坐标在扫描,所有先要求出两个纵坐标在线段树中的位置。然后,对这个区间进行标记,表明这个区间被覆盖。所以用到一测度变量,表面区间被覆盖的层数。刚开始建立线段树的时候,区间被覆盖的层数初始化化为0。想一下,一个矩形有左边和右边。所以一个区间被覆盖被删除的次数是一样的。遇到左边,这个区间要被覆盖。遇到右边,这个区间要被删除。这样可以保证计算面积的正确性。最终在计算矩形并的面积的时候,计算的是已经被离散化后的小的矩形。如果这个区间被覆盖过,那么这块区间的面积就会被计算到。如果遇到的是右边,表面这块区间的面积已经解除覆盖,没必要再算。那么计算出来的面积就会为0。这样就可得得到整个矩形并的面积了。

       第5步是我对矩形并的理解。很可能这就是这个方法叫 “离散化+线段树+扫描线",的原因吧!

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 230
struct node
{
    int st,ed,c;
    double m;
}ST[N*3];
struct Line
{
    double x,y1,y2;
    bool s;
}line[N];
double y[N],ty[N];
void buildTree(int root,int st,int ed)  //建立线段树
{
    ST[root].st=st;
    ST[root].ed=ed;
    ST[root].c=0;          //区域被覆盖的重数
    ST[root].m=0;          //区间的测度
    if(ed>1+st)
    {
        int mid=(st+ed)/2;
        buildTree(root*2,st,mid);
        buildTree(root*2+1,mid,ed);
    }
}
void updata(int root)
{
    if(ST[root].c>0)ST[root].m=y[ST[root].ed-1]-y[ST[root].st-1];
    else if(ST[root].ed-ST[root].st==1)ST[root].m=0;
    else ST[root].m=ST[root*2].m+ST[root*2+1].m;
}
void insert(int root,int st,int ed)
{
    if(st<=ST[root].st&&ST[root].ed<=ed)
    {
        ST[root].c++;
        updata(root);
        return ;
    }
    if(ST[root].ed-ST[root].st==1)return ;
    int mid=(ST[root].ed+ST[root].st)/2;
    if(st<mid)insert(root*2,st,ed);
    if(ed>mid)insert(root*2+1,st,ed);
    updata(root);
}
void Delet(int root,int st,int ed)
{
    if(st<=ST[root].st&&ST[root].ed<=ed)
    {
        ST[root].c--;
        updata(root);
        return ;
    }
    if(ST[root].ed-ST[root].st==1)return ;
    int mid=(ST[root].ed+ST[root].st)/2;
    if(st<mid)Delet(root*2,st,ed);
    if(ed>mid)Delet(root*2+1,st,ed);
    updata(root);
}
int correspond(int n,double t)
{
    int low,high,mid;
    low=0,high=n-1;
    while(low<high)
    {
        mid=(low+high)/2;
        if(t>y[mid])low=mid+1;
        else high=mid;
    }
    return high+1;
}
bool cmp(Line a,Line b)    //按照直线横坐标从小到大排序
{
    return a.x<b.x;
}
int main()
{
    double x1,y1,x2,y2;
    int n,cn=0;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)  //体会思想 离散化的思想
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i*2].s=1;
            line[i*2].x=x1;
            line[i*2].y1=y1;
            line[i*2].y2=y2;    //一个矩形平行于y轴的左边的边

            line[i*2+1].s=0;
            line[i*2+1].x=x2;
            line[i*2+1].y1=y1;
            line[i*2+1].y2=y2;  //一个矩形平行于y轴的右边的边

            ty[i*2]=y1;         //一个矩形的两个纵坐标
            ty[i*2+1]=y2;
        }
        n<<=1;                  //将n的值变为原来的二倍
        sort(line,line+n,cmp);  //排序直线
        sort(ty,ty+n);          //默认升序排列
        int num;
        y[0]=ty[0];             //将纵坐标去重
        for(int i=num=1;i<n;i++)
        {
            if(ty[i]!=ty[i-1])y[num++]=ty[i];
        }
        buildTree(1,1,num);     //建立线段树
        double area=0;
        for(int i=0;i<n-1;i++)
        {
            int l=correspond(num,line[i].y1); //确定一条线段的上下两个端点纵坐标的下标
            int r=correspond(num,line[i].y2); //扫描线的思想 + 线段树
            if(line[i].s)insert(1,l,r);       //表示矩形的左边
            else Delet(1,l,r);                //删除矩形的右边
            area+=ST[1].m*(line[i+1].x-line[i].x);
        }
        printf("Test case #%d\n",++cn);
        printf("Total explored area: %.2lf\n\n",area);
    }
    return 0;
}

这个题目,还得好好思考,总结一下!传参数的时候,千万不要把类型给搞混了,搞的我一直调试不出来。这是最让人头疼的事情。还有就是建树根更新的时候,下标要严格的对应,否则得不到正确的结果。我习惯对应于数组的下标来建立线段树习惯了数组下标从零开始,但是节点从1开始。

贴一下又写的代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 230
struct SegTree
{
    int l,r;
    int c;
    double m;
}tree[N*3];
struct Line
{
    double x;
    double y1,y2;
    bool s;
}line[2*N];
int num;
double y[2*N];
bool cmp(Line a,Line b)
{
    return a.x<b.x;
}
void BuildTree(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].c=0;
    tree[root].m=0;
    if(l+1==r)return;
    int mid=(l+r)>>1;
    BuildTree(root<<1,l,mid);
    BuildTree(root<<1|1,mid,r);
}
int bsearch(double *arr,double key)
{
    int low,high,mid;
    low=0;high=num-1;
    while(low<=high)
    {
        mid=(low+high)>>1;
        if(arr[mid]==key)return mid;
        if(arr[mid]<key)low=mid+1;
        else high=mid-1;
    }
    return 0;
}
void Updata(int root)
{
    if(tree[root].c>0)tree[root].m=y[tree[root].r]-y[tree[root].l];
    else if(tree[root].l+1==tree[root].r)tree[root].m=0;
    else tree[root].m=tree[2*root].m+tree[2*root+1].m;
}
void insert(int root, int l,int r)
{
    if(l<=tree[root].l&&tree[root].r<=r)
    {
        tree[root].c++;
        Updata(root);
        return;
    }
    if(tree[root].l+1==tree[root].r)return ;
    int mid=(tree[root].l+tree[root].r)/2;
    if(l<mid)insert(2*root,l,r);
    if(r>mid)insert(2*root+1,l,r);
    Updata(root);
}
void Delet(int root,int l,int r)
{
    if(l<=tree[root].l&&tree[root].r<=r)
    {
        tree[root].c--;
        Updata(root);
        return;
    }
    if(tree[root].l+1==tree[root].r)return;
    int mid=(tree[root].l+tree[root].r)/2;
    if(l<mid)Delet(2*root,l,r);
    if(r>mid)Delet(2*root+1,l,r);
    Updata(root);
}
int main()
{
    int n;
    double ty[2*N];
    int cn=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[2*i].s=1;
            line[2*i].x=x1;
            line[2*i].y1=y1;
            line[2*i].y2=y2;

            line[2*i+1].s=0;
            line[2*i+1].x=x2;
            line[2*i+1].y1=y1;
            line[2*i+1].y2=y2;

            ty[2*i]=y1;
            ty[2*i+1]=y2;
        }
        n<<=1;
        sort(ty,ty+n);
        sort(line,line+n,cmp);
        y[0]=ty[0];
        for(int i=num=1;i<n;i++)
        {
            if(ty[i]!=ty[i-1])
            {
                y[num++]=ty[i];
            }
        }
        BuildTree(1,0,num-1);
        double area=0;
        for(int i=0;i<n;i++)
        {
            int l=bsearch(y,line[i].y1);
            int r=bsearch(y,line[i].y2);
            if(line[i].s)insert(1,l,r);
            else Delet(1,l,r);
            area+=tree[1].m*(line[i+1].x-line[i].x);
        }
        printf("Test case #%d\n",++cn);
        printf("Total explored area: %.2lf\n\n",area);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值