首页 > 代码库 > hdu1754I Hate It

hdu1754I Hate It

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

单点更新,查找区间最大值。

 1 #include<cstdio>
 2 #include<iostream>
 3 #define lson l,m,rt<<1
 4 #define rson m+1,r,rt<<1|1
 5 using namespace std;
 6 const int maxn=200010;
 7 int f[maxn<<2];
 8 inline pushup(int rt)
 9 {
10     f[rt]=max(f[rt<<1],f[rt<<1|1]);
11 }
12 
13 void build(int l,int r,int rt)
14 {
15     if(l==r)
16     {
17         scanf("%d",&f[rt]);
18         return ;
19     }
20     int m=(l+r)>>1;
21     build(lson);
22     build(rson);
23     pushup(rt);
24 }
25 
26 void update(int p,int sc,int l,int r,int rt)
27 {
28     if(l==r) {
29         f[rt]=sc;
30         return;
31     }
32     int m=(l+r)>>1;
33     if(p<=m) update(p,sc,lson);
34     else update(p,sc,rson);
35     pushup(rt);
36 }
37 
38 int query(int L,int R,int l,int r,int rt)
39 {
40     if(L<=l&&r<=R) return f[rt];
41     int m=(l+r)>>1;
42     int ans=-1;
43     if(L<=m) ans=max(ans,query(L,R,lson));
44     if(R>m) ans=max(ans,query(L,R,rson));
45     return ans;
46 }
47 
48 int main()
49 {
50     int n,m,a,b;
51     while(scanf("%d%d",&n,&m)!=EOF)
52     {
53         build(1,n,1);
54         char s[2];
55         while(m--)
56         {
57             scanf("%s%d%d",s,&a,&b);
58             if(s[0]==Q) printf("%d\n",query(a,b,1,n,1));
59             else update(a,b,1,n,1);
60         }
61 
62     }
63     return 0;
64 }

 

hdu1754I Hate It