线段树/hdu1754 I Hate It

题意

  有n个学生一字排开,m个操作:

  Q a b 查询[a,b]中分值最高的分,并输出

  U a b 把第a个学生的成绩改为b

分析

  单点修改的裸线段树

Accepted Code

 1 /*
 2     PROBLEM:hdu1754
 3     AUTHER:Rinyo
 4     MEMO:线段树
 5 */
 6 
 7 
 8 #include<cstdio>
 9 #include<algorithm>
10 using namespace std;
11 const int maxn=200005;
12 struct node
13 {
14     int left,right,sum;
15 
16 }tree[4*maxn];
17 void pushup(int x) {tree[x].sum=max(tree[x<<1].sum,tree[x<<1|1].sum);}
18 void buildtree(int l,int r,int x)
19 {
20     if (l==r) 
21     {
22         scanf("%d",&tree[x].sum);
23         return;
24     }
25     int mid=(l+r)>>1;
26     buildtree(l,mid,x<<1);
27     buildtree(mid+1,r,x<<1|1);
28     pushup(x);
29 }
30 void update(int pos,int data,int l,int r,int x)
31 {
32     if (l==r) 
33     {
34         tree[x].sum=data;
35         return;
36     }
37     int mid=(l+r)>>1;
38     if (pos<=mid) update(pos,data,l,mid,x<<1);
39     else update(pos,data,mid+1,r,x<<1|1);
40     pushup(x);
41 }
42 int query(int a,int b,int l,int r,int x)
43 {
44     if (a<=l && r<=b) return tree[x].sum;
45     int mid=(l+r)>>1;
46     int ans=0;
47     if (a<=mid) ans=max(ans,query(a,b,l,mid,x<<1));
48     if (b>mid) ans=max(ans,query(a,b,mid+1,r,x<<1|1));
49     return ans;
50 }
51 int main()
52 {
53     //freopen("1754.txt","r",stdin);
54     int n,m;
55     while (~scanf("%d%d",&n,&m))
56     {
57         buildtree(1,n,1);
58         while (m--)
59         {
60             char ch[2];
61             int a,b;
62             scanf("%s%d%d",ch,&a,&b);
63             if (ch[0]=='Q') printf("%d\n",query(a,b,1,n,1));
64             else update(a,b,1,n,1);
65         }
66     }
67     return 0;
68 }

 

posted @ 2013-02-24 19:38  Rinyo  阅读(217)  评论(0编辑  收藏  举报