hdu 1754 也是裸的线段树

我知道了 while(~scanf("%d%d",&n,&m)) 等价于 while(scanf("%d%d",&n,&m)!=EOF)

是等价的。

而且以后用c语言输入时候的scanf("%c“,&c),这种要注意到回车空格神马的。

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #define lson l,m,rt<<1
 4 #define rson m+1,r,rt<<1|1
 5 #define maxn 880000
 6 using namespace std;
 7 int sum[maxn];
 8 void PushUp(int rt)
 9 {
10     sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
11 }
12 void build(int l,int r,int rt)
13 {
14     if(l==r)
15     {
16         scanf("%d",&sum[rt]);
17         return;
18     }
19     int m=(l+r)>>1;
20     build(lson);
21     build(rson);
22     PushUp(rt);
23 }
24 void update(int p,int c,int l,int r,int rt)
25 {
26     if(l==r)
27     {
28         sum[rt]=c;
29         return;
30     }
31     int m=(l+r)>>1;
32     if(p<=m)
33         update(p,c,lson);
34     else
35         update(p,c,rson);
36     PushUp(rt);
37 }
38 int query(int L,int R,int l,int r,int rt)
39 {
40     if(L<=l&&r<=R)
41     {
42         return sum[rt];
43     }
44     int m=(l+r)>>1;
45     int res=0;
46     if(L<=m)
47         res=max(res,query(L,R,lson));
48     if(R>m)
49         res=max(res,query(L,R,rson));
50     return res;
51 }
52 int main()
53 {
54     int m,n,i,j;
55     char c[10];
56     int a,b;
57     while(~scanf("%d%d",&n,&m))
58     {
59         build(1,n,1);
60         while(m--)
61         {
62             scanf("%s%d%d",c,&a,&b);
63             if(c[0]=='Q')
64                 printf("%d\n",query(a,b,1,n,1));
65             else 
66                 update(a,b,1,n,1);
67         }
68     }
69     return 0;
70 }
原文地址:https://www.cnblogs.com/symons1992/p/2852978.html