hdu 1754 I Hate It (线段树)

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 53991    Accepted Submission(s): 21180


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
Hint
Huge input,the C function scanf() will work better than cin
 
套的适牛模板.感谢适牛
  1 /*************************************************************************
  2     > File Name: code/hdu/1754.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年10月28日 星期三 20时34分38秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21                  
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 using namespace std;
 26 const int dx4[4]={1,0,0,-1};
 27 const int dy4[4]={0,-1,1,0};
 28 typedef long long LL;
 29 typedef double DB;
 30 const int inf = 0x3f3f3f3f;
 31 #define lson l,m,rt<<1
 32 #define rson m+1 , r , rt<<1|1
 33 const int N=2E5+7;
 34 
 35 int n,m;
 36 int tree[N<<2];
 37 void PushUp( int rt)
 38 {
 39     tree[rt]  = max(tree[rt<<1],tree[rt<<1|1]);
 40 }
 41 void build ( int l,int r,int rt)
 42 {
 43     if (l==r)
 44     {
 45     scanf("%d",&tree[rt]);
 46     return;
 47     }
 48     int m = (l+r) >> 1 ;
 49     build (lson);
 50     build(rson);
 51     PushUp(rt);
 52 }
 53 
 54 void update( int p,int sc,int l,int r,int rt)
 55 {
 56     if (l==r)
 57     {
 58     tree[rt] = sc;
 59     return;
 60     }
 61     int m =(l+r)>>1;
 62     if (p<=m) update(p,sc,lson);
 63     else update(p,sc,rson);
 64     PushUp(rt);
 65 }
 66 
 67 int query(int L ,int R, int l,int r,int rt)
 68 {
 69     if (L<=l&&r<=R)
 70     {
 71     return tree[rt];
 72     }
 73     int m = (l+r)>>1;
 74     int ret =  0;
 75     if (L<=m)
 76     {
 77     int res = query(L,R,lson);
 78     ret = max(ret,res);
 79     }
 80     if (R>m)
 81     {
 82     int res = query(L,R,rson);
 83     ret  = max(ret,res);
 84     }
 85     return ret;
 86 }
 87 int main()
 88 {
 89   #ifndef  ONLINE_JUDGE 
 90    freopen("in.txt","r",stdin);
 91   #endif
 92 
 93    while (scanf("%d %d",&n,&m)!=EOF)
 94     {
 95     build(1,n,1);
 96     while (m--)
 97     {
 98         char opt[10];
 99         int u,v;
100         scanf("%s%d%d",opt,&u,&v);
101         if (opt[0]=='Q') printf("%d\n",query(u,v,1,n,1));
102         else update (u,v,1,n,1);
103     }
104     }
105   
106    
107  #ifndef ONLINE_JUDGE  
108   fclose(stdin);
109   #endif
110     return 0;
111 }
View Code

 

 

 

posted @ 2015-10-28 21:14  111qqz  阅读(139)  评论(0编辑  收藏  举报