lintcode:线段树的修改
对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值。
设计一个 modify 的方法,接受三个参数 root、 index 和 value。该方法将 root 为跟的线段树中 [start, end] = [index, index] 的节点修改为了新的 value ,并确保在修改后,线段树的每个节点的 max属性仍然具有正确的值。
对于线段树:
[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]
如果调用 modify(root, 2, 4), 返回:
[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]
或 调用 modify(root, 4, 0), 返回:
[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) {
// write your code here
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 = Math.max(root.left.max,root.right.max);
}
}
Java Code
"""
Definition of SegmentTreeNode:
class SegmentTreeNode:
def __init__(self, start, end, max):
self.start, self.end, self.max = start, end, max
self.left, self.right = None, None
""" 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: nothing
"""
def modify(self, root, index, value):
# write your code here
if root == None:
return
if root.start == root.end:
if root.start == index:
root.max = value
return
self.modify(root.left,index,value)
self.modify(root.right,index,value)
root.max = max(root.left.max,root.right.max)
Python Code
lintcode:线段树的修改的更多相关文章
- 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 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
随机推荐
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序 先看一下我们完成的效果图 首先下载xmppframework这个框架,下载 点ZIP下载 接下来,用Xcode新建一个工程 将以下 ...
- lua编程基础
1.目前最新的lua版本是lua5.2.3 2.官网下载地址:http://www.lua.org/ftp/ 3.lua的初衷就是一个用于c/c++的小巧的脚本语言,本身是什么功能都没有的,需要手动用 ...
- asdoc 档案
1.asdoc air项目会出现无法找到NativeApplication等错误 解决办法:add args -load-config ....../frameworks/air-config.x ...
- 使用Log Explorer查看和恢复数据
由于一次意外操作,把QC数据库中的BUG表数据给删掉了.崩溃-上网找了下恢复方法,找到一款Log Explorer.下载安装使用后,发现这款软件的确不错,收藏ing. 本次的使用的Log Expl ...
- android实现左右滑动菜单
直接看效果图: 主要实现代码: package com.way.view; import android.content.Context; import android.media.Den ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- Linux进程栈和线程栈
参考资料: http://blog.csdn.net/xhhjin/article/details/7579145 总结: 1.进程的栈大小是在进程执行的时刻才能指定的,即不是在编译的时候决定 ...
- WC2016游记
出发的时候,长沙忽然就出了太阳,明媚而和煦. [day0 25] 噫吁嚱,危乎高哉!蜀道之难,难于上青天! 总之本来上午已经准备好9:50的飞机…然后就在一次次的手机查询中变成了5点多…然后也不是直飞 ...
- Hibernate O/R Mapping模拟
作为SSH中的重要一环,有必要理解一下Hibernate对 O/R Mapping的实现. 主要利用java的反射机制来得到完整的SQL语句. 准备工作: 1. Object Student实体类: ...
- Android系统软件卸载方法
最简单直接的卸载方式当然是用Re文件浏览器进系统软件目录删除即可,其次是用LBE等工具卸载系统软件.不过发现系统软件卸载后还会有桌面图标,怎么都干不掉,真是匪夷所思啊!简直就打不死的小强 系统软件装在 ...