HDU-1542 Atlantis 矩形面积并 扫描线

和hdu1255、hdu1264 一个类型 就不多做解释

详见:

1255  http://blog.csdn.net/u011742541/article/details/13298237

1264  http://blog.csdn.net/u011742541/article/details/13233973

 

#include "stdio.h"
#include "algorithm"
using namespace std;
const int maxn = 210;
int n;
double y[maxn];
struct node
{
    double x,y1,y2;    //x 边的位置  y边的区间大小 
    int f;                //f 标记 前边还是后边
}Line[maxn];
struct node1
{
    double ld,rd,len;
    int l,r,c;
}tree[maxn*4];

bool cmp( node a,node b )
{
    return a.x < b.x;
}

void buildtree( int ld,int rd,int t)  // 建树
{
    tree[t].c = 0;      tree[t].len = 0;
    tree[t].l = ld;     tree[t].r = rd;
    tree[t].ld = y[ld]; tree[t].rd = y[rd];
    if( ld+1 == rd )
        return;
    int mid = ( ld+rd )/2;
    buildtree( ld,mid,t*2 );
    buildtree( mid,rd,t*2+1 );
}

void calen( int t )             //求线段树被覆盖长度
{
    if( tree[t].c > 0 )   // t区间f是否被覆盖
    {
        tree[t].len = tree[t].rd - tree[t].ld;
        return;
    }
    if( tree[t].l +1 == tree[t].r ) tree[t].len = 0;    //
    else
        tree[t].len = tree[2*t].len + tree[2*t+1].len;    //  回带len的值
}

void updata( int t,node e )
{
    if( e.y1 == tree[t].ld && e.y2 == tree[t].rd )   //在线段树中找到边e所在的区间 更新f求出长度冷
    {
        tree[t].c += e.f;
        calen(t);
        return;
    }
    if( e.y2 <= tree[2*t].rd )   //如果e全在tree[t]的左半边
        updata( 2*t,e );
    else if( e.y1 >= tree[2*t+1].ld )  //e在tree[t]的右半边
        updata( 2*t+1,e );
    else                                //一部分在左半边 一部分在右半边
    {
        node temp = e;
        temp.y2 = tree[2*t].rd;
        updata( 2*t,temp );

        temp = e;
        temp.y1 = tree[2*t+1].ld;
        updata( 2*t+1,temp );
    }
    calen(t);
}

int main()
{
    int i,j,cas=1,pos;
    double x1,y1,x2,y2,ans;
    while( scanf("%d",&n)!=EOF , n)
    {
        pos = 0;
        for( i=1; i<=n; i++ )
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

            pos++;
            Line[pos].x = x1;                 //存前边 
            Line[pos].y1 = y1;
            Line[pos].y2 = y2;
            Line[pos].f = 1;
            y[pos] = y1;
            
            pos++;
            Line[pos].x = x2;                  //存后边
            Line[pos].y1 = y1;
            Line[pos].y2 = y2;
            Line[pos].f = -1;
            y[pos] = y2;            
        }
        sort( y+1,y+pos+1);
        sort( Line+1,Line+pos+1,cmp );
        buildtree( 1,pos,1 );
        ans = 0;
        for( i=1; i<=pos; i++ )
        {
            ans += tree[1].len * ( Line[i].x - Line[i-1].x );  //tree[1].len 表示f为1的线段树长度 Line[i].x - Line[i-1].x相邻2边的距离
            updata( 1,Line[i] );
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值