poj 3468 : A Simple Problem with Integers 【线段树 区间修改】
2017-08-11 21:01
525 查看
题目链接
题目是对一个数组,支持两种操作
操作C:对下标从a到b的每个元素,值增加c;
操作Q:对求下标从a到b的元素值之和。
具体题解参见 http://blog.csdn.net/acceptedxukai/article/details/6933446
代码来自 http://blog.csdn.net/jinglinxiao/article/details/54561770
题目是对一个数组,支持两种操作
操作C:对下标从a到b的每个元素,值增加c;
操作Q:对求下标从a到b的元素值之和。
具体题解参见 http://blog.csdn.net/acceptedxukai/article/details/6933446
代码来自 http://blog.csdn.net/jinglinxiao/article/details/54561770
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; typedef long long LL; const int maxn=1e5+7; LL lazy[maxn<<2],sum[maxn<<2]; void push_up(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void push_down(int rt,int len) { if(lazy[rt]==0) return ; lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; sum[rt<<1]+=lazy[rt]*(len-(len>>1)); sum[rt<<1|1]+=lazy[rt]*(len>>1); lazy[rt]=0; } void build(int rt,int l,int r) { if(l==r) { lazy[rt]=0; scanf("%lld",&sum[rt]); return ; } int mid=(l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); push_up(rt); } void update(int rt,int l,int r,int ul,int ur,int v) { if(ul<=l&&r<=ur) { lazy[rt]+=v; sum[rt]+=(r-l+1)*v; return ; } if(l==r) return ; push_down(rt,r-l+1); int mid=(l+r)>>1; if(ul<=mid) update(rt<<1,l,mid,ul,ur,v); if(ur>mid) update(rt<<1|1,mid+1,r,ul,ur,v); push_up(rt); } LL query(int rt,int l,int r,int ql,int qr) { if(ql<=l&&r<=qr) return sum[rt]; int mid=(l+r)>>1; push_down(rt,r-l+1); LL ret=0; if(ql<=mid) ret+=query(rt<<1,l,mid,ql,qr); if(qr>mid) ret+=query(rt<<1|1,mid+1,r,ql,qr); return ret; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { build(1,1,n); while(m--) { char op[3]; scanf("%s",op); if(op[0]=='Q') { int l,r; scanf("%d%d",&l,&r); printf("%lld\n",query(1,1,n,l,r)); } else { int l,r,c; scanf("%d%d%d",&l,&r,&c); update(1,1,n,l,r,c); } } } }
相关文章推荐
- POJ 3468 A Simple Problem with Integers(线段树区间修改)
- [省选前题目整理][POJ 3468]A Simple Problem with Integers(线段树区间修改)
- POJ 3468 A Simple Problem with Integers 线段树 区间修改
- POJ-3468-A Simple Problem with Integers(线段树区间修改)
- POJ_3468 A Simple Problem with Integers(线段树区间修改+附线段树模板)
- poj 3468 A Simple Problem with Integers(线段树区间修改+区间求和)
- POJ 3468 A Simple Problem with Integers (线段树区间修改查询)
- POJ 3468 A Simple Problem with Integers 线段树区间修改
- POJ 3468 A Simple Problem with Integers 线段树区间修改
- POJ 3468 A Simple Problem with Integers (线段树区间修改)
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
- POJ - 3468 - A Simple Problem with Integers 【线段树 区间修改】
- POJ 3468 A Simple Problem with Integers(线段树区间修改)
- 模板(线段树 + 树状数组 + 区间修改 + 区间查询)eg:POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers【线段树区间修改】
- POJ-3468-A Simple Problem with Integers(线段树区间修改+区间求和)
- A Simple Problem with Integers POJ - 3468--------线段树的区间更新解析及例题
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers(线段树——区间更新)
- [poj] 3468 A Simple Problem with Integers [线段树][区间加&求和][懒标记]