博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-1754
阅读量:6963 次
发布时间:2019-06-27

本文共 2321 字,大约阅读时间需要 7 分钟。

链接:https://vjudge.net/problem/HDU-1754

思路:

线段树模板题

代码:

//#include 
#include
#include
#include
#include
using namespace std;const int MAXN = 2e6+10;struct Node{ int value;//线段树中每个位置对应的最大值 int left,right;//位置对应的区间}node[MAXN*2];int Father[MAXN];int Max;void BuildTree(int i,int left,int right){ //构建线段树 node[i].left = left; node[i].right = right; node[i].value = 0; if (left == right) { Father[left] = i;//原数组位置对应在线段树中的结点 return; } BuildTree(i<<1,left,(int)(floor(left+right)/2.0));//构建左子树 BuildTree((i<<1)+1,(int)(floor(left+right)/2.0)+1,right);//构建右子树}void UpdateTree(int ri){ if (ri == 1) return ; int fi = ri/2; int a = node[fi<<1].value; int b = node[(fi<<1)+1].value;//当前结点和其兄弟结点 node[fi].value = max(a,b);//当前结点的父节点的最大值 UpdateTree(fi);}void Query(int i,int left,int right){ //查询区间最大值 if (node[i].left == left&&node[i].right == right) { Max = max(Max,node[i].value); return; } i <<= 1; if (left <= node[i].right)//查询区间交叉 { if (right <= node[i].right)//查询区间完全属于左结点 Query(i,left,right); else Query(i,left,node[i].right);//查询区间部分属于左节点 } i++; if (right >= node[i].left) { if (left >= node[i].left)//查询区间完全属于右结点 Query(i,left,right); else Query(i,node[i].left,right);//查询区间部分属于右结点 }}int main(){ int n,m,g; ios::sync_with_stdio(false); //while (cin >> n >> m) while (~scanf("%d%d",&n,&m)) { BuildTree(1,1,n); for (int i = 1;i<=n;i++) { scanf("%d",&g); node[Father[i]].value = g;//给只包含一个位置的结点更新值 UpdateTree(Father[i]);//初始化所有父节点 } char op[10]; int l,r; for (int i = 1;i<=m;i++) { //cin >> op >> l >> r; scanf("%s%d%d",op,&l,&r); if (op[0] == 'Q') { Max = 0; Query(1,l,r); cout << Max << endl; } else { node[Father[l]].value = r;//更新单个结点值 UpdateTree(Father[l]);//更新更改结点所有父节点 } } } return 0;}

  

转载于:https://www.cnblogs.com/YDDDD/p/10262472.html

你可能感兴趣的文章
nginx 负载均衡示例
查看>>
[原]巧用RenderTexture
查看>>
android:layout_gravity="bottom"不起作用问题
查看>>
Linux用户态程序计时方式详解
查看>>
转:dll文件解读
查看>>
博客园博客停止更新的通知,程序员生存定律会在CSDN发完
查看>>
模仿SDWebImage实现异步加载图片
查看>>
#define barrier() __asm__ __volatile__("": : :"memory") 中的memory是gcc的东西
查看>>
JAVA SE 框架之俄罗斯方块的效果
查看>>
C#正则表达式获取组名,按照组名输出匹配内容
查看>>
白话经典算法系列之五 归并排序的实现
查看>>
org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found
查看>>
8月最后一天随想
查看>>
《曾国藩发迹史》--汪衍振
查看>>
认识Swift
查看>>
OpenNMS Log Correlator
查看>>
结构体 变迁
查看>>
KMP算法具体解释(转)
查看>>
doxygen可以生成C/C++代码的文档(根据注释)
查看>>
百度地图3.0实现图文并茂的覆盖物
查看>>