I Hate It (HD_1754) 线段树
来源:互联网 发布:地图 数据可视化工具 编辑:程序博客网 时间:2024/06/07 00:55
题目描述:见杭电OJ http://acm.hdu.edu.cn/showproblem.php?pid=1754
解题思路:利用线段树(注区间更新为更新最大值)
代码如下:</pre><pre name="code" class="cpp">
//线段树 I Hate It//http://acm.hdu.edu.cn/showproblem.php?pid=1754#include"cstdio"#include"cstring"using namespace std;int _max(int a,int b){return a>b?a:b;} struct _tree{int l,r,sum;int getmid(){return ((l + r)/2);}}tree[200005*4];//构建线段树 void BulidTree(int l,int r,int pos){tree[pos].l = l;tree[pos].r = r;tree[pos].sum=0;if(l == r) return;int mid = tree[pos].getmid() ;BulidTree(l,mid,pos*2);BulidTree(mid + 1,r,pos*2 + 1);}//区间更新(包括增加和减少) void UpdataTree(int count,int x,int pos) {if(count >= tree[pos].sum )tree[pos].sum = count;if(tree[pos].l == tree[pos].r == x){tree[pos].sum == count;}//printf("pos= %d[%d,%d] %d\n",pos,tree[pos].l,tree[pos].r,tree[pos].sum);if(tree[pos].l == tree[pos].r) return ;int mid = tree[pos].getmid() ;if(x <= mid) UpdataTree(count,x,2*pos);else UpdataTree(count,x,2*pos + 1);}//区间查询int Query(int x,int y,int pos){if(tree[pos].l == x && tree[pos].r == y)return tree[pos].sum;int mid = tree[pos].getmid() ; if(y <= mid) return Query(x,y,pos*2);else if(x >mid ) return Query(x,y,pos*2+1);else return _max(Query(x,mid,pos*2),Query(mid + 1,y,pos*2+1));} int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ BulidTree(1,n,1);//生成线段树 for(int i=1;i<=n;i++){ int t; scanf("%d",&t); UpdataTree(t,i,1); } //操作 int a,b;char opt[10];while(m--){scanf("%s%d%d",&opt,&a,&b);if(!strcmp(opt,"Q")){printf("%d\n",Query(a,b,1)); }else{UpdataTree(b,a,1);}} } return 0; }
1 0
- I Hate It (HD_1754) 线段树
- I Hate It (线段树)
- 线段树:I Hate It
- i hate it 线段树
- I Hate It 【线段树】
- I Hate It---线段树
- I hate it --线段树
- I Hate It (线段树)
- 线段树i hate it
- I Hate It(线段树)
- I hate it HDU【线段树】
- 线段树--【HDU I Hate It】
- HDU 1754 I Hate It 线段树
- [hdu] 1754 I hate it -- 线段树
- hdu 1754 -- I Hate It (线段树)
- HDU 1754 I Hate It 线段树
- POJ 1745 I HATE IT(线段树)
- HDU1754 I Hate It,线段树
- 机器学习种类(三)
- 数据结构C语言版之二叉树操作
- Linux 下编译 redis 报错解决办法
- OC学习篇之-----内存管理
- 数据结构C语言版之邻接表(各种遍历)
- I Hate It (HD_1754) 线段树
- QFileDialog、QColorDialog中英文提示信息翻译方法
- 关于mybatis使用中遇到的问题集合帖(持续更新)
- 使用FEC改善UDP(RTP)音视频传输效果
- 文件操作与模板编程题 #1(C++程序设计第7周)
- Java 8 的 JVM 有多快?Fork-Join 性能基准测试
- 数据结构C语言版之邻接矩阵(遍历)
- 给马小怂写的教程-----微信如何添加表情包
- Activity之间传递Bitmap