线段树模板
2015-06-05 16:21
363 查看
#include <cstdio> #include <iostream> #include <cmath> using namespace std; const int MAXNODE = 524288; // 1<<19 const int MAXST = 200001; int father[MAXST]; struct STU { int value; int left,right; }st[MAXNODE]; void buildtree(int i,int left,int right) { st[i].value=0; st[i].left=left; st[i].right=right; if(left==right) { father[left]=i; return; } buildtree(i<<1,left,(left+right)/2); buildtree((i<<1)+1,(left+right)/2+1,right); } void updatetree(int ri)//ri为节点 { if(ri==1) { return ; } int fi=ri/2; int a = st[fi<<1].value; int b = st[(fi<<1)+1].value; st[fi].value = max(a,b); updatetree(ri/2); } int Max; void Query(int i,int l,int r) { if(st[i].left == l && st[i].right==r) { Max = max(Max,st[i].value); return ; } i=i<<1; if(l<=st[i].right) { if(r<=st[i].right) Query(i,l,r); else Query(i,l,st[i].right); } i+=1; if(r>=st[i].left) { if(l>st[i].left) Query(i,l,r); else Query(i,st[i].left,r); } } int main() { int n,q,temp; while(~scanf("%d %d",&n,&q)) { buildtree(1,1,n); for(int i=1;i<=n;i++) { scanf("%d",&temp); st[father[i]].value = temp; updatetree(father[i]); } while(q--) { char o[3]; int a,b; scanf("%s %d %d",o,&a,&b); if(o[0] == 'Q') { Max=0; Query(1,a,b); printf("%d\n",Max ); }else { st[father[a]].value=b; updatetree(father[a]); } } } return 0; }
相关文章推荐
- oc第一课
- linux serial构架分析及驱动开发(6)
- 开始一个流程
- linux serial构架分析及驱动开发(4)
- [Win32SDK基本] 窗口详解(超详细)
- 企业网盘的三大特性
- 开源地址
- java中的多线程基础
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- js中的对象封装
- android 技术点记录
- linux serial构架分析及驱动开发(3)
- Androd乐园(二)
- iOS中手工创建Cell和手工Tableview添加代理源
- iOS开发拓展篇—应用之间的跳转和数据传递
- ioctl()函数详解
- spring中使用parent属性来减少配置
- win8怎么打开或关闭快速启动(进入BIOS前的设置)
- C++--编译器工作原理
- EasyUI刚加载时候Window窗体自动弹出的解决办法