lintcode-203-线段树的修改
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-线段树的修改的更多相关文章
- lintcode:线段树的修改
线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 value.该 ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- I Hate It(线段树点修改区间查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others) ...
- 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序加上一个线 ...
- Ocean的礼物(线段树单点修改)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s Memory ...
- POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】
一.题目 POJ3468 二.分析 裸的线段树区间查询+修改. 三.AC代码 #include <cstdio> #include <iostream> #include &l ...
- 题解报告: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 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- HDU - 1754 I Hate It (线段树点修改求最大值)
题意:有N个学生M条操作,0<N<=200000,0<M<5000,要么查询某区间内学生的最高分,要么更改某学生的成绩. 分析:原理和线段树点修改求和类似. #include& ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
随机推荐
- 欧几里得算法/欧几里得扩展算法-python
说在开头. 出于对欧几里得的尊重,先简单介(cou)绍(ge)一(zi)下(shu).. 欧几里得,古希腊人,数学家.他活跃于托勒密一世时期的亚历山大里亚,被称为“几何之父”. 他最著名的著作< ...
- 20155233 2016-2017-2《Java程序设计》课程总结
20155233 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:预习去写博客,如何达到理想的师生关系. 预备作业2:对自己C语言的学习进行了解与认识. ...
- 【课堂实验】Arrays和String单元测试
实验内容 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 sort binarySea ...
- 20145209刘一阳《网络对抗》Exp9 Web安全基础实践
20145209刘一阳<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求 ...
- ubuntu下刻录优盘的命令
fdisk -l 找到优盘为/dev/sdb4 sudo dd if=/home/alex/Desktop/kali-linux-2016.1-amd64.iso of=/dev/sdb4
- ELKStack入门篇(三)之logstash收集日志写入redis
1.部署Redis 1.1.下载redis [root@linux-node2 ~]# wget http://download.redis.io/releases/redis-4.0.6.tar.g ...
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Python环境搭建和pycharm安装
Python环境搭建和pycharm安装 本人安装环境为Windows10系统,下载的Python版本为3.4社区版本,可参考 1.下载Python3.4版本 官网:https://www.pytho ...
- String、StringBuffer、StringBuilder有什么区别
区别 先说说String和StringBuffer/StringBuilder: String是标准的不可变类,是一个字符串常量池,并且声明的对象在方法中是唯一存在的. StringBuffer/St ...
- idea scala 报 with UTF-8 Please try specifying another one using the -encoding option
现象如下图, 代码里有汉字,执行代码报错,说编码格式不对, 修改方式如上面,将右下角的编码格式修改成 u8即可.