HDU1754 I Hate It 线段树入门题复习
2016-07-22 11:57
399 查看
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 63408 Accepted Submission(s): 24617
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 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。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
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
Sample Output
5
6
5
9
思路:跟上一题(区间求和)差不多,sum[now]=sum[leftson]+sum[rightson] 换成 res[now]=max(res[leftson],res[rightson]); #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=200005; int res[maxn*4]; void buildtree(int l,int r,int pos) { if(l==r){scanf("%d",&res[pos]);return;} int mid=(l+r) >>1; buildtree(l,mid,pos<<1); buildtree(mid+1,r,pos<<1|1); res[pos]=max(res[pos<<1],res[pos<<1|1]); } void update(int l,int r,int pos,int value,int end) { if(l==r){res[pos]=value;return;} int mid=(l+r)>>1; if(end<=mid)update(l,mid,pos<<1,value,end); else update(mid+1,r,pos<<1|1,value,end); res[pos]=max(res[pos<<1],res[pos<<1|1]); } int query(int l,int r,int ql,int qr,int pos) { int ans=0; int mid=(l+r)>>1; if(ql<=l&&r<=qr)return res[pos]; else { if(ql<=mid)ans=max(query(l,mid,ql,qr,pos<<1),ans); if(qr>mid)ans=max(query(mid+1,r,ql,qr,pos<<1|1),ans) } return ans; } int main() { int n,m; char q[10]; while(scanf("%d%d",&n,&m)==2) { buildtree(1,n,1); while(m--) { int a,b; scanf("%s%d%d",q,&a,&b); if(q[0]=='U') { update(1,n,1,b,a); } else if(q[0]=='Q') { printf("%d\n",query(1,n,a,b,1)); } } } }
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity