hdu 1759 I Hate It

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

存四个变量,两个边界一个sum存其成绩,max存这段的最值。在更新成绩时 父节点的max要同时更新。

View Code
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define L(x)(x<<1)
6 #define R(x)(x<<1|1)
7 #define MID(x,y) ((x+y)>>1)
8 const int MAX=200005;
9 struct Tnode{
10 int sum,left,right,max;
11 }node[MAX*4];
12 int num[MAX];
13 void init()
14 {
15 memset(node,0,sizeof(node));
16 }
17 void Build(int t,int l,int r)
18 {
19 node[t].left=l;
20 node[t].right=r;
21 if(l+1==r)
22 {
23 node[t].sum=num[l];
24 node[t].max=num[l];
25 return ;
26 }
27 int mid=MID(l,r);
28 Build(L(t),l,mid);
29 Build(R(t),mid,r);
30 node[t].max=(node[L(t)].max>node[R(t)].max)?node[L(t)].max:node[R(t)].max;
31 }
32 void update(int t,int l,int r,int sum)
33 {
34 if(node[t].left==l&&node[t].right==r)
35 {
36 node[t].sum=sum;
37 node[t].max=sum;
38 return ;
39 }
40 int mid=MID(node[t].left,node[t].right);
41 if(l>=mid) update(R(t),l,r,sum);
42 else if(r<=mid) update(L(t),l,r,sum);
43 else {
44 update(L(t),l,mid,sum);
45 update(R(t),mid,r,sum);
46 }
47 //if(node[t].max<sum)node[t].max=sum;
48 node[t].max=(node[L(t)].max>node[R(t)].max)?node[L(t)].max:node[R(t)].max;
49 }
50 int query(int t,int l,int r)
51 {
52 int gg,mm,tt;
53 if( node[t].left == l && node[t].right == r )
54 return node[t].max;
55 int mid = MID(node[t].left,node[t].right);
56 if( l >= mid )
57 return query(R(t),l,r);
58 else
59 if( r <= mid )
60 return query(L(t),l,r);
61 else
62 {
63 gg=query(L(t),l,mid);
64 mm=query(R(t),mid,r);
65 if(gg>mm)tt=gg;
66 else tt=mm;
67 return tt;
68 }
69
70 }
71 int main()
72 {
73 int ind = 1,m,n,x,y;
74 char s[10];
75
76 while(scanf("%d%d",&n,&m)!=EOF)
77 {
78 for(int i=0; i<n; i++)
79 scanf("%d",&num[i]);
80 init();
81 Build(1,0,n+1);
82
83 while(m--)
84 {
85 scanf("%s%d%d",s,&x,&y);
86
87 if(s[0] =='U' )
88 update(1,x-1,x,y);
89 else
90 printf("%d\n",query(1,x-1,y));
91 }
92 }
93 return 0;
94 }

 

posted @ 2012-02-27 17:37  我们一直在努力  阅读(253)  评论(0编辑  收藏  举报