hdu 1754 I Have It 线段树--区间最值

I Hate It

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


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
 

 

Recommend
lcy
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #define N 200010
  4 using namespace std;
  5 struct node{
  6     int Left,Right;
  7     int Value;
  8 }point[3*N];
  9 int index[N];
 10 void makeTree(int start,int end,int points)
 11 {
 12     point[points].Left=start;
 13     point[points].Right=end;
 14     point[points].Value=0;
 15     if(start==end)
 16     {
 17         index[start]=points;
 18         return ;
 19     }
 20     int middle=(start+end)>>1;
 21     makeTree(start,middle,points<<1);
 22     makeTree(middle+1,end,points<<1|1);
 23 }
 24 void updatePoint(int points,int value)
 25 {
 26     int k=index[points];
 27     if(k>0&&point[k].Value>value)
 28     {
 29         while(k>0&&point[k>>1].Value==point[k].Value)
 30         {
 31             if(k%2&&point[k-1].Value!=point[k].Value)
 32             {
 33                 point[k].Value=value;
 34                 value=max(point[k-1].Value,value);
 35             }
 36             else if(k%2==0&&point[k-1].Value!=point[k].Value)
 37             {
 38                 point[k].Value=value;
 39                 value=max(point[k+1].Value,value);
 40             }
 41             k>>=1;
 42         }
 43         point[k].Value=value;
 44     }
 45     else{
 46         while(k>0&&point[k].Value<value)
 47         {
 48             point[k].Value=value;
 49             k>>=1;
 50         }
 51     }
 52 }
 53 int seachFirst(int start,int end,int points)
 54 {
 55     if(point[points].Left==start&&point[points].Right==end)
 56     {
 57         return point[points].Value;
 58     }
 59     int middle=(point[points].Left+point[points].Right)>>1;
 60     if(start>middle)
 61     {
 62         return seachFirst(start,end,points<<1|1);
 63     }
 64     else if(end<=middle)
 65     {
 66         return seachFirst(start,end,points<<1);
 67     }
 68     else
 69     {
 70         int a=seachFirst(start,middle,points<<1);
 71         int b=seachFirst(middle+1,end,points<<1|1);
 72         return max(a,b);
 73     }
 74 }
 75 int main()
 76 {
 77     char ch;
 78     int i,n,m,score,point_left,point_right,points;
 79     while(scanf("%d%d",&n,&m)!=EOF)
 80     {
 81         makeTree(1,n,1);
 82         for(i=1;i<=n;i++)
 83         {
 84             scanf("%d",&score);
 85             updatePoint(i,score);
 86         }
 87         while(m--)
 88         {
 89             scanf("%*c%c",&ch);
 90             if(ch=='U')
 91             {
 92                 scanf("%d%d",&points,&score);
 93                 updatePoint(points,score);
 94             }
 95             else
 96             {
 97                 scanf("%d%d",&point_left,&point_right);
 98                 printf("%d\n",seachFirst(point_left,point_right,1));
 99             }
100         }
101     }
102     return 0;
103 }

 

posted @ 2012-08-14 16:54  zyh123101  阅读(125)  评论(0编辑  收藏  举报