hdu 1754 I Hate It

线段树基础题:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 0x7fffffff
 4 #define MAXN 800000
 5 int N, M, D, tree[MAXN];
 6 
 7 int max(int a, int b)
 8 {
 9     if(a>b) return a;
10     else return b;
11 }
12 
13 void updata(int cur)
14 {
15     for(int i = (D+cur>>1); i^1; i >>= 1)
16         tree[i] = max(tree[i<<1], tree[i<<1|1]);
17 }
18 
19 int query(int x, int y)
20 {
21     int i = D+x-1, j = D+y+1, ans = -INF;
22     for(; i^j^1; i >>= 1, j >>=1)
23     {
24         if(~i&1)
25             ans = max(ans, tree[i^1]);
26         if(j&1)
27             ans = max(ans, tree[j^1]);
28     }
29     return ans;
30 }
31 
32 void init()
33 {
34     while(~scanf("%d%d",&N,&M))
35     {
36         for(D = 1; D < N+2; D <<= 1);
37         memset(tree, 0xc3, sizeof(tree));
38         for(int i = 1; i <= N; i ++)
39             scanf("%d",&tree[D + i]);
40         for(int i = D-1; i > 0; i --)
41             tree[i] = max(tree[i<<1], tree[i<<1|1]);
42         for(int i = 0; i < M; i ++)
43         {
44             char s[5];
45             int a, b;
46             scanf("%s%d%d",s,&a,&b);
47             if(s[0] == 'Q')
48             {
49                 printf("%d\n",query(a,b));
50             }
51             else
52             {
53                 tree[D+a] = b;
54                 updata(a);
55             }
56         }
57     }
58 }
59 
60 int main()
61 {
62     init();
63     return 0;
64 } 
posted on 2012-08-21 09:49  BFP  阅读(153)  评论(0编辑  收藏  举报