單點更新

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

敵兵布陣 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.