http://acm.hdu.edu.cn/showproblem.php?pid=1754&&线段树区间求最值
2012-03-23 14:43
549 查看
I Hate It
[b]Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13403 Accepted Submission(s): 5188
[/b]
[align=left]Problem Description[/align]
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
[align=left]Input[/align]
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 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。
[align=left]Output[/align]
对于每一次询问操作,在一行里面输出最高成绩。
[align=left]Sample Input[/align]
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
[align=left]Sample Output[/align]
5
6
5
9
AC代码:
#include<iostream> #include<string.h> #include<algorithm> #define N 200001 #define L(x) (x<<1) #define R(x) (x<<1|1) #define M(x,y) ((x+y)>>1) using namespace std; int maxx=-0xfffff; typedef struct str { int l; int r; int key; }Node; Node node[4*N]; int value ; int bulid(int t,int l,int r) { node[t].l=l; node[t].r=r; if(l==r-1) return node[t].key=value[r]; int mid=M(l,r); return node[t].key=max( bulid(L(t),l,mid),bulid(R(t),mid,r)); } void update(int t,int l,int sum) { if(node[t].l==node[t].r-1) { node[t].key=sum; return; } int mid=M(node[t].l,node[t].r); if(l>=mid) update(R(t),l,sum); else update(L(t),l,sum); node[t].key=max(node[L(t)].key,node[R(t)].key); } int Quary(int t,int l,int r) { if(node[t].l==l&&node[t].r==r) return node[t].key; int mid=M(node[t].l,node[t].r); if(l>=mid) return Quary(R(t),l,r); else { if(r<=mid) return Quary(L(t),l,r); else return max(Quary(R(t),mid,r), Quary(L(t),l,mid)); } } int main() { int n,m; while(cin>>n>>m) { for(int i=1;i<=n;++i) cin>>value[i]; bulid(1,0,n); char ch; for(int i=0;i<m;++i) { cin>>ch; int x,y; cin>>x>>y; if(ch=='Q') cout<<Quary(1,x-1,y)<<endl; else update(1,x-1,y); } }return 0; }
动态:
#include<iostream> #include<string.h> #include<algorithm> using namespace std; typedef struct NODE { int ld; int rd; struct NODE *lc,*rc; int key; NODE() { rc=lc=NULL; } }*Node,T; int value[200001]; Node build(int a,int b) { Node p=new T; p->ld=a; p->rd=b; if(a==b-1) { p->key=value[b]; return p; } int mid=(a+b)/2; p->rc=build(mid,b); p->lc=build(a,mid); p->key=max(p->rc->key,p->lc->key); return p; } void update(Node p,int a,int b) { if(p->ld==p->rd-1) { p->key=b; return ; } int mid=(p->ld+p->rd)/2; if(a>=mid) update(p->rc,a,b); else update(p->lc,a,b); p->key=max(p->lc->key,p->rc->key); } int Quary(Node p,int a,int b) { if(p->ld==a&&p->rd==b) return p->key; int mid=(p->ld+p->rd)/2; if(a>=mid) return Quary(p->rc,a,b); else { if(b<=mid) return Quary(p->lc,a,b); else return max(Quary(p->rc,mid,b),Quary(p->lc,a,mid)); } } void delet(Node p)//后序遍历删结点 { if(p->lc) delet(p->lc); if(p->rc) delet(p->rc); delete p; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;++i) scanf("%d",&value[i]); Node p=new T; p=build(0,n); for(int i=0;i!=m;++i) { getchar(); char ch; int x,y; scanf("%c%d%d",&ch,&x,&y); if(ch=='Q') printf("%d\n",Quary(p,x-1,y)); else update(p,x-1,y); } delet(p); } return 0; }
相关文章推荐
- http://acm.hdu.edu.cn/showproblem.php?pid=2795&&线段树之求最小区间端点值
- http://acm.hdu.edu.cn/showproblem.php?pid=3308&&线段树之区间合并
- http://acm.hdu.edu.cn/showproblem.php?pid=1166&&线段树区间求和
- http://acm.hdu.edu.cn/showproblem.php?pid=1779 线段树 求区间最大值 结点更新
- http://acm.hdu.edu.cn/showproblem.php?pid=1394&&线段树之求逆序数
- 二分查找求函数的区间最小值&&http://acm.hdu.edu.cn/showproblem.php?pid=2899
- http://acm.hdu.edu.cn/showproblem.php?pid=2871 更新最左区间
- http://acm.hdu.edu.cn/showproblem.php?pid=1269&&迷宫城堡
- http://acm.hdu.edu.cn/showproblem.php?pid=1540 更新节点,询问节点所在的位置有多少连续的区间
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=2191&&多重背包+二进制优化
- http://acm.hdu.edu.cn/showproblem.php?pid=2544&&最短路
- http://acm.hdu.edu.cn/showproblem.php?pid=2844&&背包问题
- 矩阵问题&&http://acm.hdu.edu.cn/showproblem.php?pid=1575
- http://acm.hdu.edu.cn/showproblem.php?pid=4337&&dfs
- http://acm.hdu.edu.cn/showproblem.php?pid=2256&&构造矩阵求值
- Saving Princess claire_&&http://acm.hdu.edu.cn/showproblem.php?pid=4308
- http://acm.hdu.edu.cn/showproblem.php?pid=1116&&Play on Words
- Hello Kiki&&http://acm.hdu.edu.cn/showproblem.php?pid=3579
- http://acm.hdu.edu.cn/showproblem.php?pid=1878&&欧拉回路