For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval.

Implement a modify function with three parameterrootindex and value to change the node's value with[start, end] = [index, index] to the new given value. Make sure after this change, every node in segment tree still has the max attribute with the correct value.

Example

For segment tree:

                      [1, 4, max=3]
/ \
[1, 2, max=2] [3, 4, max=3]
/ \ / \
[1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=3]

if call modify(root, 2, 4), we can get:

                      [1, 4, max=4]
/ \
[1, 2, max=4] [3, 4, max=3]
/ \ / \
[1, 1, max=2], [2, 2, max=4], [3, 3, max=0], [4, 4, max=3]

or call modify(root, 4, 0), we can get:

                      [1, 4, max=2]
/ \
[1, 2, max=2] [3, 4, max=0]
/ \ / \
[1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=0]
 
 /**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
*@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
*/
public void modify(SegmentTreeNode root, int index, int value) {
changeAndUpdate(root, index, value);
} private void changeAndUpdate(SegmentTreeNode root, int index, int value) {
if (root.start == index && root.end == index) {
root.max = value;
return;
} int mid = (root.start + root.end) / ;
if (mid >= index) {
change(root.left, index, value);
} else {
change(root.right, index, value);
}
root.max = Math.max(root.left.max, root.right.max);
}
}

Segment Tree Modify的更多相关文章

  1. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  2. 203. Segment Tree Modify

    最后更新 二刷 08-Jan-2017 利用线段树来更改,找到更改的NODE,然后更改那个brach上的所有max值. 首先确定recursion的终止条件 然后通过判断大小来找方向 找到NODE之后 ...

  3. 『线段树 Segment Tree』

    更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...

  4. 关于线段树的感悟(Segment Tree)

    线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...

  5. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  6. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  7. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  8. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  9. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

随机推荐

  1. qt添加最小化和关闭按钮

    int width = this->width();//获取界面的宽度 //构建最小化.最大化.关闭按钮 QToolButton *minButton = new QToolButton(thi ...

  2. 5.9-4用字符串生成器给字符串str追加1~10这10个数字

    package zfc; public class ZfcShcq { public static void main(String[] args) { // TODO Auto-generated ...

  3. UTL_FILE详解

    包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...

  4. PostConstruct

    Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解 1.6. @PostConstr ...

  5. 【bzoj1857】 Scoi2010—传送带

    http://www.lydsy.com/JudgeOnline/problem.php?id=1857 (题目链接) 题意 给出两条线段AB和CD,在AB上的速度为P,在CD上的速度为Q,在AB,C ...

  6. 与Java Web Service相关的若干概念(JAX-WS,JAX-RS)

    WS ,JAX-WS ,JAX-RS,REST,Restlet,SOAP l  JWS: 是指与webservice相关的J2EE(其实现在应该叫做Java EE吧)技术叫做 JWS(全称就是 jav ...

  7. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试

    WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 . XAML <Image x:Name="srcImg"Width=" ...

  8. JS 在线网站

    JS 在线压缩网站 国内压缩js网站http://tool.css-js.com/ uglifyjs 压缩js网站http://lisperator.net/uglifyjs JS在线格式化网站 ht ...

  9. Stream Byte[] 转换

    public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(byt ...

  10. std::thread

    std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...