hdu4027Can you answer these queries?

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

区间(单点)更新,区间求和。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #define lson l,m,rt<<1
 5 #define rson m+1,r,rt<<1|1
 6 #define ll long long
 7 using namespace std;
 8 const int maxn=100010;
 9 ll f[maxn<<2];
10 void pushup(int rt)
11 {
12     f[rt]=f[rt<<1]+f[rt<<1|1];
13     return;
14 }
15 void build(int l,int r,int rt)
16 {
17     f[rt]=0;
18     if(l==r)
19     {
20         scanf("%I64d",&f[rt]);
21         return;
22     }
23     int m=(l+r)>>1;
24     build(lson);
25     build(rson);
26     pushup(rt);
27 }
28 
29 void update(int L,int R,int l,int r,int rt)
30 {
31     if(l==r){
32         f[rt]=sqrt(1.0*f[rt]);
33         return;
34     }
35     if(L<=l&&r<<R&&f[rt]==r-l+1) return;  //剪枝
36     int m=(l+r)>>1;
37     if(L<=m) update(L,R,lson);
38     if(R>m) update(L,R,rson);
39     pushup(rt);
40 }
41 
42 ll query(int L,int R,int l,int r,int rt)
43 {
44     if(L<=l&&r<=R) return f[rt];
45     int m=(l+r)>>1;
46     ll ans=0;
47     if(L<=m) ans+=query(L,R,lson);
48     if(R>m) ans+=query(L,R,rson);
49     return ans;
50 }
51 int main()
52 {
53     int n;
54      int cas=0;
55     while(scanf("%d",&n)!=EOF)
56     {
57         build(1,n,1);
58         int m;
59         scanf("%d",&m);
60        
61          printf("Case #%d:
",++cas);
62         for(int i=1;i<=m;i++)
63         {
64 
65             int a,b,c;
66             scanf("%d%d%d",&a,&b,&c);
67             int x=max(b,c);
68             int y=min(b,c);
69             if(a==0) update(y,x,1,n,1);
70             else printf("%I64d
",query(y,x,1,n,1));
71         }
72         puts("");
73     }
74 }
原文地址:https://www.cnblogs.com/yijiull/p/6619179.html