poj3468 线段树成段更新模板
2012-08-21 15:42
274 查看
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define clear(a) memset(a,0,sizeof(a)) const int N=101000; __int64 col[N<<2],sum[N<<2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void pushdown(int l,int r,int rt) { int m; __int64 k; if (col[rt]) { k=col[rt]; m=(l+r)>>1; sum[rt<<1]+=(m-l+1)*k; sum[rt<<1|1]+=(r-m)*k; col[rt<<1]+=k; col[rt<<1|1]+=k; col[rt]=0; } } void build(int l,int r,int rt) { if (l==r) { scanf("%I64d",&sum[rt]); col[rt]=0; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt); } __int64 query(int L,int R,int l,int r,int rt) { //pushdown(l,r,rt); if (L<=l&&r<=R) { return sum[rt]; } pushdown(l,r,rt); int m=(l+r)>>1; __int64 res=0; if (L<=m) res+=query(L,R,lson); if (R>m) res+=query(L,R,rson); return res; } void update(int L,int R,int add,int l,int r,int rt) { //pushdown(l,r,rt); if (L<=l&&r<=R) { sum[rt]+=(r-l+1)*add; col[rt]=add; return; } pushdown(l,r,rt); int m=(l+r)>>1; if (L<=m) update(L,R,add,lson); if (R>m) update(L,R,add,rson); pushup(rt); } int main() { int n,m,i,x,y,z; while (scanf("%d%d",&n,&m)!=EOF) { clear(sum); clear(col); build(1,n,1); while (m--) { char c[3],ch; scanf("%s",c); ch=c[0]; scanf("%d%d",&x,&y); if (ch=='Q') { __int64 res=query(x,y,1,n,1); printf("%I64d\n",res); } else { scanf("%d",&z); update(x,y,z,1,n,1); } } } return 0; }
相关文章推荐
- 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst
- 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst
- poj3468(线段树成段更新模板题)
- poj3468 线段树多段更新学习
- 线段树成段更新1002 POJ3468
- poj3468 A Simple Problem with Integers(线段树成段更新)
- 线段树成段更新模板
- 线段树模板 poj3468
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
- poj3468 线段树成段更新水题
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- POJ3468 区间加,区间求和 线段树模板
- Poj3468 线段树 -- 成段更新
- POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】
- poj3468 A Simple Problem with Integers(线段树+区间更新+非完全替换)模板
- [POJ3468]线段树模板
- 线段树成段更新裸题POJ3468
- poj 3468 A Simple Problem with Integers 线段树成段更新模板
- 线段树成段更新裸题POJ3468
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers