POJ 3468 A Simple Problem with Integers (线段树区间更新模板题)
2017-04-10 21:39
519 查看
题目链接
POJ3468题目大意
给N个数,Q个操作(1 ≤ N,Q ≤ 100000)有两种操作:
1.”C a b c”:Aa, Aa+1, … , Ab都加c
2.”Q a b”:求Aa, Aa+1, … , Ab的和
分析
线段树区间更新模板题。代码
#include <iostream> #include <cstring> #include <string> #define LL long long using namespace std; const int MAXN=100010; LL a[MAXN],ans[MAXN<<2],lazy[MAXN<<2]; void PushUp(int rt) { ans[rt]=ans[rt<<1]+ans[rt<<1|1]; } void Build(int l,int r,int rt) { if (l==r) { ans[rt]=a[l]; return; } int mid=(l+r)>>1; Build(l,mid,rt<<1); Build(mid+1,r,rt<<1|1); PushUp(rt); } void PushDown(int rt,int ln,int rn) { if (lazy[rt]) { lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; ans[rt<<1]+=lazy[rt]*ln; ans[rt<<1|1]+=lazy[rt]*rn; lazy[rt]=0; } } void Update(int L,int R,int C,int l,int r,int rt) { if (L<=l&&r<=R) { ans[rt]+=C*(r-l+1); lazy[rt]+=C; return; } int mid=(l+r)>>1; PushDown(rt,mid-l+1,r-mid); if (L<=mid) Update(L,R,C,l,mid,rt<<1); if (R>mid) Update(L,R,C,mid+1,r,rt<<1|1); PushUp(rt); } LL Query(int L,int R,int l,int r,int rt) { if (L<=l&&r<=R) return ans[rt]; int mid=(l+r)>>1; PushDown(rt,mid-l+1,r-mid); LL ANS=0; if (L<=mid) ANS+=Query(L,R,l,mid,rt<<1); if (R>mid) ANS+=Query(L,R,mid+1,r,rt<<1|1); return ANS; } int main() { ios::sync_with_stdio(false); int n,q,i,x,y,z; string op; cin>>n>>q; for (i=1;i<=n;i++) cin>>a[i]; memset(ans,0,sizeof(ans)); memset(lazy,0,sizeof(lazy)); Build(1,n,1); while (q--) { cin>>op; if (op=="C") { cin>>x>>y>>z; Update(x,y,z,1,n,1); } if (op=="Q") { cin>>x>>y; cout<<Query(x,y,1,n,1)<<endl; } } return 0; }
相关文章推荐
- 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 B - 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(区间更新,求和)-splay或线段树
- POJ 3468 :A Simple Problem with Integers——区间更新线段树经典题目
- poj 3468 A Simple Problem with Integers(线段树的区间更新与求和)
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)