传奇广告查询第一站 同步54.com

[lintcode]203. 线段树的修改
原创 于2026-01-05 18:07:00发布
9 阅读
0
0

链接:https://www.lintcode.com/zh-cn/problem/segment-tree-modify/#


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

设计一个 modify 的方法,接受三个参数 rootindexvalue。该方法将 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]
class Solution { public: /** * @param root: The root of segment tree. * @param index: index. * @param value: value * @return: nothing */ void modify(SegmentTreeNode * root, int index, int value) { // write your code here if(!root) return; if(indexstart || index>root->end) return; if(root->start==root->end && root->end==index) root->max=value; else { modify(root->left,index,value); modify(root->right,index,value); root->max=max(root->left->max,root->right->max); } } };


管理员
0
0
0
分享
上一篇: 【新1.80热血金币复古三职业[白猪3]】2025整理特色服务端+锁妖塔+龙骨之地+地下宫殿【站长亲测】
下一篇: 【传奇手游之三国辉煌八大陆[白猪3]免授权版】单职业复古特色战神引擎传奇手游-最新打包Win服务端源码视频...
评论
历史记录
回顶部
浏览时间 游戏名称 游戏IP 开区网址
注册GM1论坛账号
  • 上传头像
注册

已有账号,

微信扫码登录
重置密码
重置密码

注册

绑定关联手机号
关联手机号