首页 > 代码库 > 单点更新

单点更新

敌兵布阵 http://acm.hdu.edu.cn/showproblem.php?pid=1166

 1 #include<cstdio> 2 #define lrrt int L,int R,int rt 3 #define iall 1,n,1 4 #define imid int mid=(L+R)>>1 5 #define lson L,mid,rt<<1 6 #define rson mid+1,R,rt<<1|1 7 const int M=50010; 8 int tree[M<<2]; 9 int a[M];10 void pushup(int rt){11     tree[rt]=tree[rt<<1]+tree[rt<<1|1];12 }13 void build(lrrt){14     if(L==R){15         tree[rt]=a[L];16         return ;17     }18     imid;19     build(lson);20     build(rson);21     pushup(rt);22 }23 void update(int x,int y,lrrt){24     if(L==R){25         tree[rt]+=y;26         return ;27     }28     imid;29     if(mid>=x) update(x,y,lson);30     else       update(x,y,rson);31     pushup(rt);32 }33 int query(int x,int y,lrrt){34     if(x<=L&&R<=y) return tree[rt];35     imid;36     int ans=0;37     if(mid>=x) ans+=query(x,y,lson);38     if(mid<y)  ans+=query(x,y,rson);39     return ans;40 }41 int main(){42     int t,n;43     while(~scanf("%d",&t)){44         int cas=1;45         while(t--){46             printf("Case %d:\n",cas++);47             scanf("%d",&n);48             for(int i=1;i<=n;i++){49                 scanf("%d",&a[i]);50             }51             build(iall);52             char op[8];53             while(~scanf("%s",op)){54                 if(op[0]==E) break;55                 int x,y;56                 scanf("%d%d",&x,&y);57                 if(op[0]==A){58                     update(x,y,iall);59                 }60                 else if(op[0]==S){61                     update(x,-y,iall);62                 }63                 else{64                     printf("%d\n",query(x,y,iall));65                 }66             }67         }68     }69     return 0;70 }
View Code

 

I Hate It http://acm.hdu.edu.cn/showproblem.php?pid=1754

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

 

 

 

 

end