线段树之HDU1754I Hate It
2015-08-10 09:24
281 查看
也是改点求段,只需要将update里的目标值替换,然后pushup换成求max即可。
#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <math.h> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int MAXN=200010; int sum[MAXN<<2]; int a[MAXN]; inline void PushUp(int rt)//向上更新保留最大值 { sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r) { sum[rt]=a[l]; return; } int m=(l+r)>>1; build(lson); build(rson); PushUp(rt); } void update(int id,int add,int l,int r,int rt) { if(l==r) { sum[rt]=add;//替换 return; } int m=(l+r)>>1; if(id<=m) update(id,add,lson); else update(id,add,rson); PushUp(rt); } int query(int L,int R,int l,int r,int rt) { if( L<=l && r<=R ) return sum[rt]; int m=(l+r)>>1; if(R<=m) return query(L,R,lson); else if(L>m) return query(L,R,rson); else return max(query(L,R,lson),query(L,R,rson)); } int main() { int T; int cas; int i,j; char s[50]; int x,y; int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) scanf("%d",a+i); build(1,n,1); for(i=0;i<m;i++) { scanf("%s%d%d",s,&x,&y); if(strcmp(s,"Q")==0) { printf("%d\n",query(x,y,1,n,1)); } else { update(x,y,1,n,1); } } } return 0; }
相关文章推荐
- 计蒜客 难题题库 034 灌溉
- 变形课
- Python之Window环境搭建
- Reuse Is About People and Education, Not Just Architecture
- C#与SQLite数据库
- 艰难的食物链的解题报告
- 如何不装Oracle就能连上企业内部数据库
- ios培训
- 喷水装置(一) nyoj 6 && 喷水装置(二)NYOJ_12
- 高性能Mysql主从架构的复制原理及配置详解
- 设计模式之-----命令模式
- Warning: Problems in Mirror May Be Larger Than They Appear
- linux c 笔记 网络编程(三)..套接字数据传输
- bzoj2743
- 如何抓微信的请求
- Java4Android-接口
- 最小生成树 Prime算法
- cocos2dx添加文本的三种方法及适用情况CCLabelTTF,CCLabelBMFont,CCLabelAtlas
- 计蒜客 难题题库 033 三个数的和
- hdu5339Untitled