线段树 自己总结的模板
来源:互联网 发布:java优缺点 编辑:程序博客网 时间:2024/05/17 03:46
struct treeNode {
int start;
int end;
int cover; // 覆盖标记 -1:未覆盖 0:部分覆盖 1:完全覆盖
treeNode *left;
treeNode *right;
treeNode *parent;
};
//构造线段树函数
treeNode *createTree(int start,int end)
{
treeNode * p =new treeNode();
if (end-start==1) {
returnNULL;
}
p->start = start;
p->end=end;
p->cover=-1;
p->parent=NULL;
// 生成左子树
p->left=createTree(start, (start+end)/2);
p->left->parent=p;
// 生生右子树
p->right=createTree((start+end)/2, end);
p->right->parent=p;
return p;
}
//覆盖线段树函数
void coverTree(treeNode* p,int start,int end)
{
if (p->cover==1)return;
//完全覆盖
if (start==p->start&&end==p->end) {
p->cover=1;
//父段存在的话,回溯、重新判断覆盖情况
treeNode *temp = p;
while (temp->parent!=NULL) {
if (temp->parent->cover==-1) temp->parent->cover=0;
if (temp->parent->cover==0) temp->parent->cover=1;
temp=temp->parent;
}
//按照我的设计,根结点的parent为NULL
if (temp->cover==-1) temp->cover=0;
if (temp->cover==0) temp->cover=1;
}
if (end <= (p->start+p->end)/2) {//向左子树覆盖
coverTree(p->left, start, end);
}
else if (start >=(p->start+p->end)/2)//向右子树覆盖
{
coverTree(p->right, start, end);
}
else{ //向左、右子树覆盖
//先向左子树覆盖
coverTree(p->left, start, (p->start+p->end)/2);
//后向右子树覆盖
coverTree(p->right, (p->start+p->end)/2, end);
}
- 线段树 自己总结的模板
- 线段树总结 与 模板
- 线段树---分析 && 模板总结
- C++ 线段树—模板&总结
- HDU 4819 Mosaic(二维线段树单点更新+区间查询+自己的写法模板)
- 线段树的代码模板
- Duan2baka的线段树模板!
- 自己总结的 Makefile 小模板
- 线段树的总结
- 线段树模板(学长给的,自己消化了一下,其实基本上差不多(好吧,一模一样QAQ))
- 线段树模板以及对线段树的理解
- 线段树的实现(模板)
- 有关线段树的基本操作模板
- 线段树的模板(更新ing)
- ACM 线段树模板(模板)
- 线段树的总结1
- 线段树的一点总结
- 线段树模板
- java中File类的判断功能
- 一个简单的检查十个数字中是否有重的小程序
- 将Ado.net中的事务抽离到业务层
- JdbcBaseRevpiew系列之JdbcBatch(二)--批处理
- 微信自带分享功能实现
- 线段树 自己总结的模板
- 重构前的分析
- 如何利用github打造博客专属域名
- LintCode 解题记录 2017.6.3
- Spark的模式挖掘—FPGrowth算法
- HDU1576 A/B(扩展欧几里得)
- 如何用Python快速爬下拉勾招聘信息
- FFT
- java中File类的获取功能和修改名字功能