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): 65040    Accepted Submission(s): 25255


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
 

 

Author
linle
 

 

Source
 
今天决定还是先把线段树的知识点水一遍,然而智障的我一开始又被多组数据坑了QAQ……
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 
13 #define lson rt<<1,l,m
14 #define rson rt<<1|1,m+1,r
15 
16 const int MAX=200005;
17 int n,m;
18 int Max[MAX<<2];
19 
20 void PushUp(int rt){Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);}
21 
22 void build(int rt,int l,int r){
23     if (l==r)
24     {scanf("%d",&Max[rt]);
25      return;
26     }
27     int m=(l+r)>>1;
28     build(lson);
29     build(rson);
30     PushUp(rt);
31 }
32 void update(int rt,int l,int r,int x,int y){
33     if (l==r)
34     {Max[rt]=y;
35      return;
36     }
37     int m=(l+r)>>1;
38     if (x<=m)
39      update(lson,x,y);
40     else
41      update(rson,x,y);
42     PushUp(rt);
43 }
44 int query(int rt,int l,int r,int x,int y){
45     if (x<=l && r<=y)
46      return Max[rt];
47     int m=(l+r)>>1;
48     int ans(0);
49     if (x<=m)
50      ans=max(ans,query(lson,x,y));
51     if (y>m)
52      ans=max(ans,query(rson,x,y));
53     return ans;
54 }
55 int main(){
56     freopen ("hate.in","r",stdin);
57     freopen ("hate.out","w",stdout);
58     int i,j;
59     int x,y;
60     char c;
61     while (scanf("%d%d",&n,&m)!=EOF){
62     build(1,1,n);
63     while (m--)
64     {c=getchar();
65      while (!(c=='Q' || c=='U')) c=getchar();
66      scanf("%d%d",&x,&y);
67      if (c=='Q')
68       printf("%d\n",query(1,1,n,x,y));
69      if (c=='U')
70       update(1,1,n,x,y);
71     }}
72     return 0;
73 }

 

posted @ 2016-08-07 10:23  lonely_OIer  阅读(110)  评论(0编辑  收藏  举报