HDU 1754I Hate It(线段树)
来源:互联网 发布:淘宝衣服好评语100字 编辑:程序博客网 时间:2024/05/03 20:30
此题和1166敌兵布阵差不多,只需改一下输入格式,把求和改为求最大值;现在发现一旦弄明白后,这类题都是水题了,呵呵
题目大意:在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
题目解析:用到两个函数,update(),query(()函数;每次求最大值;
#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define MAX 200005struct segtree{ int l,r,max;}tree[4*MAX];//数组要开大一些int n,y[MAX];void CreatTree(int s,int e,int t)//创建{ tree[t].l=s; tree[t].r=e; tree[t].max=0; int temp=t<<1; if(s==e) { tree[t].max=y[e]; } else { int mid=(s+e)>>1; CreatTree(s,mid,temp); CreatTree(mid+1,e,temp+1);//mid+1 tree[t].max=tree[temp].max>tree[temp+1].max?tree[temp].max:tree[temp+1].max; //找出左,右子树的最大值 }}void Update(int pos,int t,int valu)//更新{ int temp=t<<1; if(tree[t].l==pos&&tree[t].r==pos) { tree[t].max=valu; } else { int mid=(tree[t].l+tree[t].r)>>1; if(pos<=mid) Update(pos,temp,valu); else Update(pos,temp+1,valu); tree[t].max=tree[temp].max>tree[temp+1].max?tree[temp].max:tree[temp+1].max; }}int Query(int s,int e,int t){ int temp=t<<1; if(s<=tree[t].l&&e>=tree[t].r) { return tree[t].max; } else { int mid=(tree[t].r+tree[t].l)>>1; int max1=0,max2=0; if(s<=mid) max2=Query(s,e,temp); if(e>mid) max1=Query(s,e,temp+1); return max1>max2?max1:max2; }}int main(){ int m,i,j,a,b; char str[2]; while(scanf("%d%d",&n,&m)!=EOF) { for(j=1;j<=n;j++) scanf("%d",&y[j]); CreatTree(1,n,1); while(m--) { scanf("%s",str); scanf("%d%d",&a,&b); if(strcmp(str,"U")==0)Update(a,1,b); else printf("%d\n",Query(a,b,1)); } } return 0;}
- HDU 1754 I Hate It 线段树
- [hdu] 1754 I hate it -- 线段树
- hdu 1754 -- I Hate It (线段树)
- HDU 1754 I Hate It 线段树
- 线段树 hdu 1754 I Hate It
- HDU 1754 I Hate It(线段树)
- hdu 1754 (线段树)I Hate It
- hdu-1754 I Hate It 线段树
- HDU--1754 -- I Hate It [线段树]
- 【线段树】hdu 1754 I hate it
- hdu - 1754 I Hate It(线段树)
- HDU--1754--I Hate It--线段树
- HDU-1754 I Hate It 线段树
- hdu 1754 I Hate It 线段树
- HDU-1754-I Hate It(线段树)
- HDU 1754 I Hate It (线段树)
- hdu 1754 I hate it(线段树)
- 【HDU】 1754 I Hate It 线段树
- HDU 4325 Vampire Numbers
- eclipse 调试 mapreduce 程序
- 序关系计数问题
- uva 674 - Coin Change(dp)
- <Android开发代码库>achartengine实现柱状图、圆饼图、线图等
- HDU 1754I Hate It(线段树)
- 最小生成树(Prim和Kruskal)
- mysql 事务
- WaveInOpen
- PowerBuilder学习笔记(4)数据窗口(DataWindow)
- Linux C调用C++库
- winform使用webbrowser 相关
- UVa 11137 - Ingenuous Cubrency
- 每天一点linux指令【一】