【线段树】HDU1754-I hate it

单点修改,区间最值的标程,没什么好说的。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 #define lson l,m,root<<1
 9 #define rson m+1,r,root<<1|1
10 const int MAXN=200000+50;
11 int n,m;
12 int maxn[MAXN*4];
13 
14 void pushUP(int root)
15 {
16     maxn[root]=max(maxn[root<<1],maxn[root<<1|1]);
17 }
18 
19 void build(int l,int r,int root)
20 {
21     if (l==r)
22     {
23         scanf("%d",&maxn[root]);
24         return;
25     }
26     int m=(l+r)>>1;
27     build(lson);
28     build(rson);
29     pushUP(root);
30 }
31 
32 void update(int p,int delta,int l,int r,int root)
33 {
34     if (l==r)
35     {
36         maxn[root]=delta;
37         return;
38     }
39     int m=(l+r)>>1;
40     if (p<=m) update(p,delta,lson);
41     if (p>m) update(p,delta,rson);
42     pushUP(root);
43 }
44 
45 int query(int L,int R,int l,int r,int root)
46 {
47     if (l>=L && r<=R)
48     {
49         return maxn[root];
50     }
51     int m=(l+r)>>1,res=-1;
52     if (L<=m) res=max(res,query(L,R,lson));
53     if (R>m) res=max(res,query(L,R,rson));
54     pushUP(root);
55     return res;
56 }
57 
58 int main()
59 {
60     while (scanf("%d%d",&n,&m)!=EOF)
61     {
62         build(1,n,1);
63         for (int i=0;i<m;i++)
64         {
65             getchar();
66             char c;
67             int a,b;
68             scanf("%c%d%d",&c,&a,&b);
69             if (c=='Q') cout<<query(a,b,1,n,1)<<endl;
70             if (c=='U') update(a,b,1,n,1);
71         }
72     }
73     return 0;
74 }

 

posted @ 2015-10-06 08:52  iiyiyi  阅读(189)  评论(0编辑  收藏  举报