线段树成段更新的模板

#include <iostream> #include <cstdio> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxx=100002; struct{ __int64 sum; int col; }Tree[maxx<<2]; void pushUp(__int64 rt){ Tree[rt].sum=Tree[rt<<1].sum+Tree[rt<<1|1].sum; } void build(int l,int r,int rt){ Tree[rt].col=0; if(l==r){ scanf("%I64d",&Tree[rt].sum);//刚开始这地方输入忘记了是六十四位整数输入 wa了两次 return ; } int m=(l+r)>>1; build(lson); build(rson); pushUp(rt); } void pushDown(int rt,int m){ if(Tree[rt].col){ Tree[rt<<1].col+=Tree[rt].col;//以下都为在原有的基础上增加 Tree[rt<<1|1].col+=Tree[rt].col; Tree[rt<<1].sum+=(__int64)Tree[rt].col*(m-(m>>1)); Tree[rt<<1|1].sum+=(__int64)Tree[rt].col*(m>>1); Tree[rt].col=0; } } void update(int L,int R,int c,int l,int r,int rt){ if(L<=l && r<=R){ Tree[rt].sum+=(r-l+1)*(__int64)c; Tree[rt].col+=c; return ; } pushDown(rt,r-l+1); int m=(l+r)>>1; if(L<=m)update(L,R,c,lson); if(R>m)update(L,R,c,rson); pushUp(rt); } __int64 query(int L,int R,int l,int r,int rt){ if(L<=l && r<=R){ return Tree[rt].sum; } pushDown(rt,r-l+1); int m=(l+r)>>1; __int64 ret=0; if(L<=m)ret+=query(L,R,lson); if(R>m)ret+=query(L,R,rson); return ret; } int main(){ int n,o; scanf("%d%d",&n,&o); build(1,n,1); while(o--){ char op[3]; int x,y,c; scanf("%s",op); if(op[0]=='Q'){ scanf("%d%d",&x,&y); printf("%I64d\n",query(x,y,1,n,1)); }else{ scanf("%d%d%d",&x,&y,&c); update(x,y,c,1,n,1); } } return 0; }
http://poj.org/problem?id=3468
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值