Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1754

刚开始学线段树

View Code
 1 //hdu 1754
2 #include <stdio.h>
3 const int N=200000;
4 int st[4*N];
5 int max(int a,int b)
6 {
7 return a>b?a:b;
8 }
9 void update(int rt)
10 {
11 st[rt]=max(st[rt*2],st[rt*2+1]);
12 }
13 void build(int l,int r,int rt)
14 {
15 if (l==r)
16 {
17 scanf("%d",&st[rt]);
18 return;
19 }
20 int m=(l+r)/2;
21 build(l,m,rt*2);
22 build(m+1,r,rt*2+1);
23 update(rt);
24 }
25 void renew(int p,int a,int l,int r,int rt)
26 {
27 if (l==r)
28 {
29 st[rt]=a;
30 return;
31 }
32 int m=(l+r)/2;
33 if (p<=m) renew(p,a,l,m,rt*2);
34 else renew(p,a,m+1,r,rt*2+1);
35 update(rt);
36 }
37 int query(int a,int b,int l,int r,int rt)
38 {
39 if (a<=l && r<=b) return st[rt];
40 int ans=0,m=(l+r)/2;
41 if (a<=m) ans=max(ans,query(a,b,l,m,rt*2));
42 if (b>m) ans=max(ans,query(a,b,m+1,r,rt*2+1));
43 return ans;
44 }
45 int main()
46 {
47 int n,m;
48 while (~scanf("%d%d",&n,&m))
49 {
50 build(1,n,1);
51 while (m--)
52 {
53 char op[2];
54 int a,b;
55 scanf("%s%d%d",op,&a,&b);
56 if (op[0]=='U') renew(a,b,1,n,1);
57 else printf("%d\n",query(a,b,1,n,1));
58 }
59 }
60 return 0;
61 }

 

posted on 2012-02-14 20:06  Qiuqiqiu  阅读(118)  评论(0编辑  收藏  举报