hdu1754线段树单点更新
2016-03-18 19:53
393 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1754
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int maxn=200005; int MAXN[maxn<<2]; void pushup(int rt) { MAXN[rt]=max(MAXN[rt<<1],MAXN[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r) { scanf("%d",&MAXN[rt]); return ; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1); pushup(rt); } void update(int p,int x,int l,int r,int rt) { if(l==r) { MAXN[rt]=x; return ; } int m=(l+r)>>1; if(p<=m) update(p,x,l,m,rt<<1); else update(p,x,m+1,r,rt<<1|1); pushup(rt); } int search(int ll,int rr,int l,int r,int rt) { if(ll<=l&&r<=rr) return MAXN[rt]; int m=(r+l)>>1; int ret=0; if(ll<=m) ret=max(ret,search(ll,rr,l,m,rt<<1)); if(rr>m) ret=max(ret,search(ll,rr,m+1,r,rt<<1|1)); return ret; } int main() {int n,m; while(~scanf("%d%d",&n,&m)) { build(1,n,1); char str[2]; int a,b; while(m--) { scanf("%s%d%d",str,&a,&b); if(str[0] == 'Q') printf("%d\n",search(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; }
相关文章推荐
- C:数据结构与算法之顺序表
- 蓝桥杯 历届试题 大数乘法
- Lintcode 用栈实现队列
- 使用yum只获取rpm包不自动安装
- C#写UTF8文件时指定是否含BOM头
- 软件测试实验——junit, hamcrest 和 eclemma的安装及使用
- JS中选择DOM元素的方法集锦
- Java课程“新”得(二)
- bzoj1022 小约翰的游戏John
- 线段树 && 树状数组
- 百度在线编辑器ueditor的使用
- dpkg查看软件包和共享库信息
- 【Settings】设置何时启动屏保
- Intervals
- 坦克,学校与教师,共同森林
- linux基础指令(三)scp
- fuel 5.1在vmware esxi上的部署
- mydumper myloader表分块导入导出
- HDOJ 1094 A+B for Input-Output Practice (VI)
- 20. Valid Parentheses