线段树的一个好的实现,包括测度独立线段数的计算

本文详细介绍了线段树数据结构的实现与应用,包括初始化、更新、插入和删除操作的具体步骤。通过实例演示了如何利用线段树解决区间查询与更新问题,并提供了完整的C语言源代码。
#include  < stdio.h > 
#include  
< stdlib.h > 
 
 
const   int  maxn  =   5010 ;
 
// 写一个线段树的过程 
 struct  Lines_tree
  
{
    Lines_tree  
*  lchild,  *  rchild;
     
int  m;  // 测度 
      int  cnt;    // count 
      int  lines;  // 连续段数 
      int  lbd, rbd;  // 左右端点是否被覆盖  
      int  f, r;  // 左右端点 
 }
 ;
Lines_tree 
*  root;
 
struct  rec  int  x, y, x1, y1;} r[maxn];
 
struct  Line
  
{
     
int  x, y1, y2; int  sign;
    Line( 
int  a,  int  b,  int  c, int  d):x(a), y1(b), y2(c), sign(d)  {} 
     Line( 
void ):x( 0 ),y1( 0 ),y2( 0 ),sign( 0 )  {}  
}
 line[ 2 * maxn + 10 ];
 
int  nr;
 
int  ans;

 
void  make_tree( int  a,  int  b, Lines_tree *  node)
  
{
    node 
-> lines  =   0 ; node -> m  =   0 ; node -> cnt  =   0 ;
    node  
->  lbd  =   0 ; node  ->  rbd  =   0 ;
    node 
-> lchild  =  NULL; node -> rchild  =  NULL;
    node 
-> f  =  a; node -> r  =  b;
     
if (b - a > 1 )
      
{
        node 
-> lchild  =   new  Lines_tree;
        make_tree(a, (a 
+ b) / 2 , node -> lchild);
        node 
-> rchild  =   new  Lines_tree;
        make_tree((a 
+ b) / 2 , b, node -> rchild);
    }
 
}
 
 
 
void  make( int  a,  int  b)
  
{
     root  
=   new  Lines_tree;
     make_tree(a, b, root);
}
 
 
 
void  update(Lines_tree  *  now)    // lbd, rbd, m的计算都在这个里面! 
   {
     
if (now -> cnt > 0 ) now -> m  =  now -> r - now -> f;
     
else   if (now -> r == now -> f + 1 ) now -> m  =   0 ;
     
else  now -> m  =  now -> lchild -> m  +  now -> rchild -> m;
}
 
 
 
void  update2(Lines_tree *  now)
  
{
     
if (now -> cnt > 0 )   { now -> lbd  =   1 ; now -> rbd  =   1 ; now -> lines  =   1 ; } 
      
else   if (now -> f + 1 == now -> r)   {now -> lbd  =   0 ; now -> rbd  =   0 ; now -> lines  =   0 ;} 
     
else 
       
{
        now 
-> lbd  =  now -> lchild -> lbd; now -> rbd  =  now -> rchild -> rbd;
        now 
-> lines  =  now -> lchild -> lines + now -> rchild -> lines  -  now -> lchild -> rbd * now -> rchild -> lbd;
    }
 
}
 
 
 
void  insert( int  a,  int  b, Lines_tree  *  now)
  
{
     
if (a <= now -> f  &&  b >= now -> r)
        now 
-> cnt ++ ;
     
if (now -> r - now -> f > 1 )
      
{
         
if (a < (now -> f + now -> r) / 2 )    insert(a, b, now -> lchild);
         
if (b > (now -> f + now -> r) / 2 )    insert(a, b, now -> rchild);
    }
 
    update(now);
    update2(now);
}
 
 
 
void  del( int  a,  int  b, Lines_tree  *  now)
  
{
     
if (a <= now -> f  &&  b >= now -> f)
      
{
         
if (a == now -> f) now -> lbd  =   0 ;
         
if (b == now -> r) now -> rbd  =   0 ;
        now 
-> cnt -- ;
    }
 
     
if (now -> r - now -> f > 1 )
      
{
         
if (a < (now -> f + now -> r) / 2 )    del(a, b, now -> lchild);
         
if (b > (now -> f + now -> r) / 2 )    del(a, b, now -> rchild);
    }
 
    update(now);
    update2(now);
}
 
 
 
int  cmp( const   void   *  a,  const   void   *  b)
  
{
     
return  ( * (Line * )a).x  -  ( * (Line * )b).x;    // 这里不要写成-> 
 }
 
 
 
void  init()
  
{
     
// initiation
     
// input 
      int  i;
    scanf( 
" %d " ,  & nr);
     
for (i = 0 ; i < nr; i ++ )
      
{
        scanf( 
" %d%d%d%d " ,  & r[i].x,  & r[i].y,  & r[i].x1,  & r[i].y1);
        line[ 
2 * i]  =  Line(r[i].x, r[i].y, r[i].y1,  0 );
        line[ 
2 * i + 1 ]  =  Line(r[i].x1, r[i].y, r[i].y1,  1 );
    }
         
    qsort(line, nr 
* 2 ,  sizeof (line[ 0 ]), cmp);
     
// pretreatment 
 }
 
 
 
void  work()
  
{
     
int  nowM  =   0 ;
     
int  nowLine  =   0 ;
     
int  lastM  =   0 ;
     
int  lastLine  =   0 ;
     
int  i;
     
for (i = 0 ; i < nr * 2 ; i ++ )
      
{
         
if (line[i].sign == 0 )
            insert(line[i].y1, line[i].y2, root);
         
else  del(line[i].y1, line[i].y2, root);
        nowM  
=  root -> m;
        nowLine  
=  root -> lines;
        ans  
+=  lastLine  *   2   *  (line[i].x - line[i - 1 ].x);
        ans  
+=  abs(nowM - lastM);
        lastM  
=  nowM;
        lastLine  
=  nowLine;
    }
 
}
 
 
 
void  output()
  
{
    printf( 
" %d  " , ans);
}
 
 
 
int  main()
  
{
 
//     freopen("t.in", "r", stdin); 
     make( - 10000 ,  10000 );
    init();
    work();
    output();
     
return   0 ;
}
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值