POJ 1754 线段树
e,应该是线段树里的水题。线段树单点更新。查询区间最值。
代码套用模板 PS :模板有些地方不太懂。
#include<stdio.h>
#include<iostream>
#include<string.h>
#define maxn 200010
using namespace std;
int val[maxn];
struct Node
{
int max; // 需要求最值这里就改成最值
int left, right;
}tree[maxn*4]; // 据说。线段树节点需要是数组的3~4倍大
int max(int a, int b)
{
return a > b ? a : b;
}
int build(int root, int left, int right) // 以root为根节点 left 和 right 为数组长度的树
{
tree[root].left = left;
tree[root].right = right;
if (left == right)
return tree[root].max = val[left]; // 给叶子节点赋值
int mid = (left + right) / 2;
int a = build(2*root, left, mid); // 建立左子树和右子树
int b = build(2*root+1, mid+1, right);
return tree[root].max = max(a, b);
}
int update(int root, int pos, int val) //单点更新 把pos 位置的元素更新为val
{
if (tree[root].left == pos && tree[root].right == pos) // 如果找到了该位置对应的节点
return tree[root].max = val;
if (tree[root].left > pos || tree[root].right < pos) //如果不是。以上是两种递归突破口。e .但是我不太明白这两种情况返回值为什么是这样的。
return tree[root].max;
int a, b;
a = update(2*root, pos, val); // 更新左子树和右子树
b = update(2*root+1, pos, val);
return tree[root].max = max(a, b);
}
int calculate(int root, int left, int right) // 求取区间left 到 right中的最大值
{
if (tree[root].left > right || tree[root].right < left) // 如果当前区间和所求区间没有交集
return 0;
if (left <= tree[root].left && tree[root].right <= right) // 如果当前区间完全包含在所求区间里。不明白为什么就要返回当前区间的最大值。
return tree[root].max;
int a, b;
a = calculate(2*root, left, right); // 上述两种情况都木有的话。需要分解所求区间,求子区间的最值。
b = calculate(2*root+1, left, right);
return max(a, b);
}
int main()
{
int n, m;
while(cin >> n >> m)
{
for (int i=1; i<=n; ++i)
scanf("%d", &val[i]);
build(1, 1, n);
while(m--)
{
char temp;
int a, b;
cin >> temp >> a >> b;
if (temp == 'Q')
cout << calculate(1, a, b) << endl;
else update(1, a, b);
}
}
return 0;
}
POJ 1754 线段树的更多相关文章
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
- hdu 1754 线段树(Max+单点修改)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU(1754),线段树,单点替换,区间最值
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- poj 2828 线段树
http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 而且后来的也能够确定----假设从前往后 ...
- POJ - 1177 线段树
POJ - 1177 扫描线 这道题也算是一道扫描线的经典题目了. 只不过这道题是算周长,非常有意思的一道题.我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长. ...
- poj 2886 (线段树+反素数打表) Who Gets the Most Candies?
http://poj.org/problem?id=2886 一群孩子从编号1到n按顺时针的方向围成一个圆,每个孩子手中卡片上有一个数字,首先是编号为k的孩子出去,如果他手上的数字m是正数,那么从他左 ...
随机推荐
- CSAPP 第三章 读书笔记
程序的机器级表示 AT&T与Intel格式的汇编代码 我们的表述是ATT(根据"AT&T"命名的, AT&T是运营贝尔实验室多年的公 司)格式的汇编代码,这 ...
- node包管理工具--nvm(windows)
windows 安装nvw-windows 使用nvm工具: windows使用nvm-noinstall.zip安装 nvm-noinstall.zip 这个是绿色免安装版本,但是使用之前需要配置 ...
- 使用qmlscene预览qml文件
功能:可以预览qml文件的界面 使用:qmlscene myapp.qml
- MFC使用MsComm做串口通信
一.注册MSCOMM 1.下载控件MSCOMM32.OCX(32位),mswnisck.ocx(64位) 2.把这个ocx文件放在c盘WIndows的system32里,打开cmd执行:regsvr3 ...
- script 加载顺序问题的延展研究
今天群里有人问为什么会出现脚本的加载顺序与定义脚本顺序不一致的问题,这个问题引起了我的好奇,经过一番调研,有了这篇文章. 这是一个伪命题吗? 首先,W3C 推荐 script 脚本应该被立即加载和执行 ...
- 将kali linux装入U盘 制作随身携带的kali linux
一 准备工作 USB3.0 U盘 不小于32G USB2.0的U盘安装速度要比3.0的慢一倍以上,运行也会有明显差别,所以建议使用3.0U盘.安装好之后差不多就得占用十几G,所以16G的太小了,尽量用 ...
- HDU 4734 (数位DP)题解
思路: dp[pos][pre]代表长度为pos的不大于pre的个数 #include<iostream> #include<cstdio> #include<cstri ...
- 同步TreeView中父结点和子结点的状态[以及Treeview的bug]
此代码仅仅是二级结点,即父结点下面只有一层子结点,只有两层结构 /// <summary> /// 某个结点的CheckBox被选中 /// </summary> /// &l ...
- poj 1144 Network 无向图求割点
Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...
- python 斐波那契查找
def fibonacci_search(lis, key): # F = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987 ...