HDU 1754 线段树单点更新 区间最值
2015-06-02 23:16
357 查看
<pre name="code" class="cpp">#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 200001; struct { int l,r,m; }nod[MAXN * 4];///总线段长度为MAXN 数组一般开到总长度的4倍 int a[MAXN]; void build(int t,int l,int r) { nod[t].l = l; nod[t].r = r; if(l == r) { nod[t].m = a[l]; return ; } int mid = (l + r) / 2; build(t << 1,l,mid); build(t << 1|1,mid+1,r); nod[t].m = max(nod[t << 1].m,nod[t << 1|1].m); } void update(int t,int n,int v) { if(nod[t].l == nod[t].r && nod[t].l == n) { nod[t].m = v; return ; } int mid = (nod[t].l + nod[t].r) / 2; if(n <= mid) update(t << 1,n,v); else update(t << 1|1,n,v); nod[t].m = max(nod[t << 1].m,nod[t << 1|1].m); } int query(int t,int L,int R) { if(L == nod[t].l && R == nod[t].r) return nod[t].m; int s; if(R <= nod[t << 1].r) s = query(t << 1,L,R);///若所查找区间在左儿子 else if(L >= nod[t << 1|1].l) s = query(t << 1|1,L,R);///若所查找区间在右儿子 else s = max(query(t << 1,L,nod[t << 1].r),query(t << 1|1,nod[t << 1|1].l,R)); ///所查找区间横跨左右儿子区间 return s; } int main() { int n,m,i,x1,x2; char s[2]; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } build(1,1,n); while(m--) { scanf("%s%d%d",s,&x1,&x2); if(s[0]=='Q') printf("%d\n",query(1,x1,x2)); else update(1,x1,x2); } } return 0; }
相关文章推荐
- CSV格式读写
- Javascript:谈谈JS的全局变量跟局部变量
- Reverse Linked List[LeetCode]
- linux c/c++ 动态库和静态库的生成与使用
- iis url重写模块 2 Rewrite
- 用Maven创建第一个web项目
- jQuery 尺寸
- 使用eject 退出光盘,重新挂载报错
- oc的指针是什么
- android中回调机制,小demo可以理解
- 自定义View视图
- Struts2 技术内幕 读书笔记(第二章)
- Find the all applications of ubuntu
- 所谓的HTTP请求,到底都干了什么
- java基础基本数据类型对象包装
- BZOJ2818 Gcd(欧拉函数)
- 解读 Oracle 12c 的 12 个新特性
- Task 9 从用户界面和体验分析“360极速浏览器”
- iOS开发学习第十一课——动态内存分配
- SIP协议结构——SIP学习笔记(三)