线段树
来源:互联网 发布:农业银行软件官方下载 编辑:程序博客网 时间:2024/06/05 08:25
线段树(segment tree)
转自:点击打开链接
下图就为一个线段树:
1.线段树基本概念
线段树是一棵二叉树,记为T(a, b),参数a,b表示区间[a,b],其中b-a称为区间的长度,记为L。
线段树中的结点一般采取如下数据结构:
struct Node{ int left,right; //区间左右值 Node *leftchild; Node *rightchild; };
2.线段树基本操作
线段树的建立:
Node *build(int l , int r ) //建立二叉树{ Node *root = new Node; root->left = l; root->right = r; //设置结点区间 root->leftchild = NULL; root->rightchild = NULL; if ( l +1< r ) { int mid = (r+l) >>1; root->leftchild = build ( l , mid ) ; root->rightchild = build ( mid , r) ; } return root; }
线段树插入一条线段[c,d]:
void Insert(int c, int d , Node *root ){ if(c<= root->left&&d>= root->right) root-> cover++; else { if(c < (root->left+ root->right)/2 )
Insert (c,d, root->leftchild ); if(d > (root->left+ root->right)/2 )
Insert (c,d, root->rightchild ); }}
线段树删除一条线段[c,d]:
void Delete (int c , int d , Node *root ){ if(c<= root->left&&d>= root->right) root-> cover= root-> cover-1; else { if(c < (root->left+ root->right)/2 )
Delete ( c,d, root->leftchild ); if(d > (root->left+ root->right)/2 )
Delete ( c,d, root->rightchild ); }}
0 0
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- ConcurrentHash Map 深入分析 (锁分离,提高效率,final key ,next域 保持线程安全)
- hd 2009 求数列的和
- For Fans of Statistics——vector
- Objective-c中的占位符,打印BOOL类型数据
- virtualbox for mac安装
- 线段树
- HDU 4911 Inversion(归并求逆序对)
- OGengine学习之一 开发环境搭建
- oralce共享连接和专用连接
- Windows下zlib库和libPng库的编译和使用
- 好的软件人员必看的书
- xmlhttp post传值servlet获取不到值
- TOMCAT数据源连接配置
- 杭电2036————任意求多边形的面积