线段树(完全二叉树)
来源:互联网 发布:2016流行网络语言 编辑:程序博客网 时间:2024/06/06 19:09
本文转自http://blog.csdn.net/cnyali/article/details/47314123
#include<stdio.h>#include<string.h>#include<stdlib.h>const int maxn=200000+10;struct node{int s;int l,r;};struct node tree[maxn*10];int a[maxn];int max(int x,int y){return x>y?x:y;}int create_tree(int h,int x,int y){tree[h].l=x;tree[h].r=y;int mid=(x+y)/2;if(x==y){tree[h].s=a[x];return tree[h].s;}int x1=create_tree(h*2,x,mid);int x2=create_tree(h*2+1,mid+1,y);tree[h].s=max(x1,x2);return tree[h].s;}int dfs(int h,int x,int y){if(y<tree[h].l || x>tree[h].r)return 0;if(x<=tree[h].l && y>=tree[h].r)return tree[h].s;int x1=dfs(2*h,x,y);int x2=dfs(2*h+1,x,y);return max(x1,x2);}int update(int h,int x){if(x<tree[h].l || x>tree[h].r)return tree[h].s;if(tree[h].l==tree[h].r){tree[h].s=a[tree[h].l];return tree[h].s;}int x1=update(2*h,x);int x2=update(2*h+1,x);tree[h].s=max(x1,x2);return tree[h].s;}int main(){int i,j,k,m,n;int x,y;char c;while(scanf("%d%d",&n,&m)!=EOF){for(i=1;i<=n;i++)scanf("%d",&a[i]);create_tree(1,1,n);for(i=1;i<=m;i++){getchar();scanf("%c %d %d",&c,&x,&y);if(c=='Q'){printf("%d\n",dfs(1,x,y));}else{ a[x]=y;update(1,x);}}}return 0;}
1 0
- HDU1754 线段树 (完全二叉树)
- 线段树(完全二叉树)
- HDU1166 敌兵布阵 树状数组|线段树-构造完全二叉树(理解)
- 【完全版】线段树
- 线段树 完全版
- 【完全版】线段树
- 【完全版】线段树
- 【完全版】线段树
- 【完全版】线段树
- 【完全版】线段树
- [完全版] 线段树
- 【完全版】线段树
- 【完全版】线段树
- 【完全版】线段树
- 【完全版】线段树
- 线段树完全版
- 【完全版】线段树
- 树:二叉树几种形态(满二叉,完全二叉,线索二叉,二叉排序,平衡二叉,哈夫曼)
- ___87__UseBrush_GDI使用画刷
- linux shell 编程14 获取时间
- 个人软件、开发工具等 临时备忘
- 118. Pascal's Triangle
- mina学习笔记二:从官方例子开始
- 线段树(完全二叉树)
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- BFS模板
- 单例模式的七种写法
- DDMS在安卓测试中常见应用
- Sql Server数据库锁相关知识
- mina学习笔记三:一切的源头IoService
- python UserAgent
- 全程软件测试