203-线段树的修改

对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值。

设计一个 modify 的方法,接受三个参数 root、 index 和 value。该方法将 root 为跟的线段树中 [start, end] = [index, index] 的节点修改为了新的 value ,并确保在修改后,线段树的每个节点的 max 属性仍然具有正确的值。

注意事项

在做此题前,最好先完成线段树的构造和 线段树查询这两道题目。

样例

对于线段树:



如果调用 modify(root, 2, 4), 返回:



或 调用 modify(root, 4, 0), 返回:

挑战

时间复杂度 O(h) , h 是线段树的高度

标签

LintCode 版权所有 二叉树 线段树

思路

利用递归, 自底向上 修改线段树

code

/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
*@param root, index, value: The root of segment tree and
*@ change the node's value with [index, index] to the new given value
*@return: void
*/
void modify(SegmentTreeNode *root, int index, int value) {
// write your code here
if (root == NULL) {
return;
}
if (root->start == root->end) {
if (root->start == index) {
root->max = value;
}
return;
}
modify(root->left, index, value);
modify(root->right, index, value); root->max = max(root->left->max, root->right->max);
}
};

lintcode-203-线段树的修改的更多相关文章

  1. lintcode:线段树的修改

    线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 value.该 ...

  2. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  3. I Hate It(线段树点修改区间查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others) ...

  4. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  5. Ocean的礼物(线段树单点修改)

    题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s      Memory ...

  6. POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】

    一.题目 POJ3468 二.分析 裸的线段树区间查询+修改. 三.AC代码 #include <cstdio> #include <iostream> #include &l ...

  7. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  8. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  9. HDU - 1754 I Hate It (线段树点修改求最大值)

    题意:有N个学生M条操作,0<N<=200000,0<M<5000,要么查询某区间内学生的最高分,要么更改某学生的成绩. 分析:原理和线段树点修改求和类似. #include& ...

  10. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

随机推荐

  1. 记一次学习PHP中的错误

    今天学习PHP时,回想起一段代码 <?php> $i = true; $o = true; $p = false; if($i or $o and $p){ echo '输出为'.'tru ...

  2. 如何在HHDI中调用Java文件

    创建执行JS语句任务,在脚本中输入相关代码,两种写法: ------------------------写法一 // 直接调用类的路径 var now = com.haohe.utils.DateUt ...

  3. Python调用time模块设置当前时间-指定时间

    import datetimeimport time#新建元旦时间#将程序打包def A(): # 设定时间 newyear =datetime.datetime(2033,10,1) #调用当前时间 ...

  4. jquery ajax实例教程和一些高级用法

    jquery ajax的调用方式:jquery.ajax(url,[settings]),jquery ajax常用参数:红色标记参数几乎每个ajax请求都会用到这几个参数,本文将介绍更多jquery ...

  5. Flex4中的拖动技术

    下面列一个最简单的例子,在Flex中,拖动原来如此简单 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?xm ...

  6. 2017-2018-1 20155308&20155321&20155330《信息安全技术》实验三——数字证书应用1

    2017-2018-1 20155308&20155321&20155330<信息安全技术>实验三--数字证书应用1 实验目的 了解PKI体系 了解用户进行证书申请和CA颁 ...

  7. 20155330 2016-2017-2《Java程序设计》课程总结

    20155330 2016-2017-2<Java程序设计>课程总结 目录 每周作业链接汇总 实验报告链接汇总 代码托管 课堂项目实践 课程收获与不足 问卷调查 给出你的总结中涉及到的链接 ...

  8. js获取IE版本,while代码很特别

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. KDTable如何添加合计行?

    /** * 功能:添加合计行 * * @param table * 指定的KDTable * @param fields * 需要合计的列 */ public static void apendFoo ...

  10. Swift - 重写导航栏返回按钮

    // 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...