Segment Tree Modify
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 parameterroot, index 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的更多相关文章
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- 203. Segment Tree Modify
最后更新 二刷 08-Jan-2017 利用线段树来更改,找到更改的NODE,然后更改那个brach上的所有max值. 首先确定recursion的终止条件 然后通过判断大小来找方向 找到NODE之后 ...
- 『线段树 Segment Tree』
更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...
- 关于线段树的感悟(Segment Tree)
线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 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), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
随机推荐
- java多线程-Exchanger
简介: 可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给exchange方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为Synchr ...
- java ee 中文乱码的问题
java ee 中文乱码的问题 发生中文乱码的三种情况 (一) 表单form Post 方法 直接在服务器中设置 request.setCharacterEncoding("utf-8&qu ...
- [转]DBA,SYSDBA,SYSOPER三者的区别
原文地址:http://www.oracleonlinux.cn/2010/02/dba_sysdba_sysoper/ 什么是DBA?什么是SYSDBA,什么又是SYSOPER?三者究竟有何联系呢? ...
- sql-char和varchar,nvarchar的区别
数据类型的比较 char表示的是固定长度,最长n个字 varchar表示的是实际长度的数据类型 比如:如果是char类型,当你输入字符小于长度时,后补空格:而是varchar类型时,则表示你输入字符的 ...
- P1391 走廊泼水节
时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 话说,中中带领的OIER们打算举行一次冬季泼水节,当然这是要秘密进行的,绝对不可以让中中知道.不过中中可是老 ...
- javax/faces/webapp/FacesServlet
严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component ...
- configure new linux
vim http://www.cnblogs.com/wswang/p/5088078.html zsh sh -c "$(curl -fsSL https://raw.github. ...
- Linux下安装Nginx详细图解教程
什么是Nginx? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下N ...
- web classpath 路径说明
classpath路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此,所以在开发时常将相应的xm ...
- The C Programming Language (second edition) 实践代码(置于此以作备份)
1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...