[hiho 19]单点修改的线段树
问题描述
依然是RMQ问题。过程需要修改某些点的数据。
使用线段树解决。
单点修改的复杂度是O(logn),即树深,只需要修改这个点的祖先节点。
区间查询的复杂度也是O(logn),因为除了第一步可能一分为二外,其他查询若有分解则分解后必然有一个区间是不需要再分解的。
#include <stdio.h>
#include <algorithm>typedef
struct _seg_tree_ {int left, right;int val;_seg_tree_ *lson = NULL, *rson = NULL;_seg_tree_(int left_idx, int right_idx, int value): left(left_idx), right(right_idx), val(value) {}
} seg_tree, *pseg_tree;pseg_tree construct_seg_tree(int left, int right) {if (left == right) {int val;scanf("%d", &val);return new seg_tree(left, left, val);}int mid = left + (right - left) / 2;pseg_tree lson = construct_seg_tree(left, mid);pseg_tree rson = construct_seg_tree(mid + 1, right);int val = std::min(lson->val, rson->val);pseg_tree ans = new seg_tree(left, right, val);ans->lson = lson;ans->rson = rson;return ans;
}int query_seg_tree(pseg_tree proot, int left, int right) {if (left == proot->left && right == proot->right) {return proot->val;}int mid = proot->left + (proot->right - proot->left) / 2;if (left > mid) {return query_seg_tree(proot->rson, left, right);}if (right <= mid) {return query_seg_tree(proot->lson, left, right);}return std::min(query_seg_tree(proot->lson, left, mid),query_seg_tree(proot->rson, mid + 1, right));
}void modify_seg_tree(pseg_tree proot, int left, int val) {if (left == proot->left && left == proot->right) {proot->val = val;return;}int mid = proot->left + (proot->right - proot->left) / 2;if (left > mid) {modify_seg_tree(proot->rson, left, val);} else {modify_seg_tree(proot->lson, left, val);}proot->val = std::min(proot->lson->val, proot->rson->val);
}int main(){int n, q;scanf("%d", &n);pseg_tree proot = construct_seg_tree(1, n);scanf("%d", &q);int op, left, right;while (q--) {scanf("%d%d%d", &op, &left, &right);if (op == 0) {int ans = query_seg_tree(proot, left, right);printf("%d\n", ans);} else {modify_seg_tree(proot, left, right);}}return 0;
}
转载于:https://www.cnblogs.com/xblade/p/4503101.html
[hiho 19]单点修改的线段树相关推荐
- FZU Problem 2240 Daxia Suneast's problem(博弈+[单点更新,区间查询]线段树)
此文章可以使用目录功能哟↑(点击上方[+]) FZU Problem 2240 Daxia & Suneast's problem Accept: 0 Submit: 0 Time L ...
- CodeforcesBeta Round #19 D. Points 离线线段树 单点更新 离散化
题目链接: http://codeforces.com/contest/19/problem/D 题意: 有三种操作"add x y"往平面上添加(x,y)这个点,"re ...
- Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)
recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...
- 1631 小鲨鱼在51nod小学(线段树区间修改+单点查询:不用下传lazy的区间修改)
题目描述: 1631 小鲨鱼在51nod小学 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务. 每一个职务都有一个起始时间A和结束时间B, ...
- 树状数组的建树 单点修改 单点查询 区间修改 区间查询
单点修改 单点查询 用普通数组就能写出来 单点修改 区间查询 用线段树 树状数组: 区间修改 区间查询 用线段树 树状数组: 区间修改 单点查询 用线段树 树状数组: 建 ...
- 【转载】线段树题目2
1.hdu1166 敌兵布阵 更新节点,区间求和. 2.hdu1754 I Hate It 更新节点,区间最值. . 3.hdu1698 Just a Hook 成段更新,总区间求和. . 4.hdu ...
- l2-004 这是二叉搜索树吗? (25分)_什么是 “线段树” ?
线段树是一个复杂的数据结构,比较难理解,也比较难解释清楚.在我将这个数据结构反复学习了五遍的时候,我终于有了信心写出这篇介绍线段树的文章.希望大家能够掌握这种数据结构. 这篇文章比较长,建议大家耐心阅 ...
- CodeForces - 343D Water Tree(树链剖分+线段树)
题目链接: 题目大意:给出一棵由n个点组成的树,初始时每个点的权值为0,接下来有m个操作,每个操作分为以下三种: 1 x:将包括节点x在内的所有子孙节点的权值都改为1 2 x:将包括节点x在内的所有父 ...
- 【BZOJ1012】最大数maxnumber,疯狂CE的线段树练习
传送门 思路:比较裸的线段树,单点修改区间查询,树状数组应该也可以做,不过上来CE不能忍(╯‵□′)╯︵┻━┻,最后发现read不能定义数组?只好改成orz了 代码: /*************** ...
最新文章
- 构造函数不能为虚/重载函数总结
- android面试题总结加强版
- Executor 执行器接口笔记
- Tensorflow在python3.7版本的运行并且在pycharm安装tensorflow(亲测)
- mysql node null_node-mysql中防止SQL注入的方法总结
- sass webpack_如何在Visual Studio和Webpack中编译Sass文件
- 带你上手全新版本的Webpack 5
- c++ 删除文件_win7 系统电脑C盘哪些文件可以删除掉?
- 9.linux ntp服务器搭建
- excel vlookup多个条件匹配多列_Excel教程第12课:VLOOKUP函数近似匹配到底怎么回事,原理+操作...
- 激烈讨论:我身边的IT认证
- The Maven Integration requires that Eclipse be running in a JDK……
- 【2019百度之星初赛二1002=HDU6675】度度熊与排列(贪心)
- 区块链上的中国?2018见分晓
- 事大发了!小助理告诉我:小伙伴21天斩获字节offer的关键竟是这份面试题!
- 解决:word文档中插入照片有一部分不显示
- python pip gpl_Python PICOS包_程序模块 - PyPI - Python中文网
- 主数据管理平台功能模型介绍
- while和do...while循环测试总结
- 我国软件昂首阔步向前走