其他分享
首页 > 其他分享> > I Hate It HDU - 1754

I Hate It HDU - 1754

作者:互联网

原题链接

考察:线段树 or 树状数组

思路:

        套模板即可.

注意:build函数里,赋值不是赋值a[u],而是a[l].l、r是1~n范围内.u是线段树结点编号.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 const int N = 200010;
 7 int n,m,a[N];
 8 char op[2];
 9 struct Node{
10     int l,r,maxn;
11 }tr[N*4];
12 int mmax(int a,int b)
13 {
14     return a>b?a:b;
15 }
16 void pushup(int u)
17 {
18     tr[u].maxn = mmax(tr[u<<1].maxn,tr[u].maxn);
19     tr[u].maxn = mmax(tr[u<<1|1].maxn,tr[u].maxn);
20 }
21 void build(int u,int l,int r)
22 {
23     tr[u].l = l,tr[u].r = r;
24     if(l==r) tr[u].maxn = a[l];
25     else{
26         int mid = l+r>>1;
27         build(u<<1,l,mid);
28         build(u<<1|1,mid+1,r);
29         pushup(u);
30     }
31 }
32 void modify(int u,int idx,int x)
33 {
34     if(tr[u].l==tr[u].r) tr[u].maxn = x;
35     else{
36         int mid = tr[u].l+tr[u].r>>1;
37         if(idx<=mid) modify(u<<1,idx,x);
38         else modify(u<<1|1,idx,x);
39         pushup(u);
40     }
41 }
42 int query(int u,int l,int r)
43 {
44     if(tr[u].l>=l&&tr[u].r<=r) return tr[u].maxn;
45     else{
46         int mid = tr[u].l+tr[u].r>>1,res = 0;
47         if(l<=mid) res = mmax(query(u<<1,l,r),res);
48         if(r>mid) res = mmax(query(u<<1|1,l,r),res);
49         return res;
50     }
51 }
52 int main()
53 {
54     while(scanf("%d%d",&n,&m)!=EOF)
55     {
56         memset(tr,0,sizeof tr);
57         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
58         build(1,1,n);
59         int x,y;
60         while(m--)
61         {
62             scanf("%s%d%d",op,&x,&y);
63             if(op[0]=='Q') printf("%d\n",query(1,x,y));
64             else modify(1,x,y);
65         }
66     }
67     return 0;
68 }
69  

 

标签:mmax,HDU,include,int,tr,maxn,build,1754,Hate
来源: https://www.cnblogs.com/newblg/p/14479718.html