Lintcode203 Segment Tree Modify solution 题解

【题目描述】

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 parameter root,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.

Notice

We suggest you finish problem Segment Tree Build and Segment Tree Query first.

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

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

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

【题目链接】

www.lintcode.com/en/problem/segment-tree-modify/

【题目解析】

此题依然可使用递归。

非叶子节点的max是其左右子节点的max值中大的那个。所以先递归找到start和end都等于index的叶子节点,更新其max为value,在逐级弹栈时更新每个节点的max值。

注意:若树的结点范围为0~n,那么至少有n/2个数在左子树上,所以if语句里用了<=号。另外,最后一句root.max = Math.max(root.left.max, root.right.max)是调用递归之后的结果,必须写在最后面。

【参考答案】

www.jiuzhang.com/solutions/segment-tree-modify/

作者:程风破浪会有时
链接:https://www.jianshu.com/p/57b56a77c947
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/5245a/p/8450402.html

时间: 2024-11-09 15:47:13

Lintcode203 Segment Tree Modify solution 题解的相关文章

Lintcode202 Segment Tree Query solution 题解

[题目描述] For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding Segment Tree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start to end). 对于一个

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] 

Lintcode: 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 parameter root, index and value to change the node's value with [start, end] = [index, index

Lintcode7 Binary Tree Serialization solution 题解

[题目描述] Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'. 设计一个算法,并编写代码来序列化

Lintcode206 Interval Sum solution 题解

[题目描述] Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the sum number between index start and end in the given array, return the re

Lintcode205 Interval Minimum Number solution 题解

[题目描述] Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the minimum number between index start and end in the given array, return th

HDU 1166 敌兵布阵 Segment Tree题解

本题是最基本的分段树操作了.或者一般叫线段树,不过好像和线段没什么关系,只是分段了. 不使用lazy标志,更新只是更新单点. 如果不使用分段树,那么更新时间效率只需要O(1),使用分段树更新效率就需要O(lgn)了. 但是不是用分段树,那么查询的时间效率是O(n),而分段树查询效率是O(lgn) 这就是amortize分摊了时间,而且lgn真的很快,数据不是非常巨大的时候,接近常数了. 故此本题需要使用分段树. #include <cstdio> class EnemyInfo { stati

HDU 1754 I hate it 分段树Segment Tree题解

给出一段数据,然后要更新单个数据,会询问一段数据中的最大值. 又是一道分段树操作.渐渐熟手了. #pragma once #include <cstdio> #include <algorithm> using namespace std; class IHateIt_1754_1 { static const int SIZE = 200001; int *segTree; //不要使用segTree[SIZE<<2] inline int lChild(int r)

lintcode-medium-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 parameter root, index and value to change the node's value with [start, end] = [index, index