成段加值更新 区间求和 poj3468 A Simple Problem with Integers
来源:互联网 发布:即时通讯软件排行 编辑:程序博客网 时间:2024/06/05 10:38
题目链接 poj3468 A Simple Problem with Integers
题目大意:给你n个附有起始值的数,然后m次询问。C a b val :表示区间[a,b]的数都加上一个val值,Q a b :询问区间[a,b]的综合。
解题思路:O(-1)
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define lz 2*u,l,mid#define rz 2*u+1,mid+1,rconst int maxn=100005;__int64 a[maxn]; ///还要注意范围__int64 tree[4*maxn];__int64 flag[4*maxn];void push_up(int u){ tree[u]=tree[2*u]+tree[2*u+1];}void push_down(int u, int l, int r){ if(flag[u]) { int mid=(l+r)>>1; flag[2*u]+=flag[u]; ///!!! flag[2*u+1]+=flag[u]; ///!!! tree[2*u]+=flag[u]*(mid-l+1); tree[2*u+1]+=flag[u]*(r-mid); flag[u]=0; }}void build(int u, int l, int r){ flag[u]=0; if(l==r) { tree[u]=a[l]; return ; } int mid=(l+r)>>1; build(lz); build(rz); push_up(u);}void Update(int u, int l, int r, int tl, int tr, __int64 val){ if(tl<=l&&r<=tr) { tree[u]+=val*(r-l+1); flag[u]+=val; ///!!!加上一个值 并非重新赋新值 return ; } push_down(u,l,r); int mid=(l+r)>>1; if(tr<=mid) Update(lz,tl,tr,val); else if(tl>mid) Update(rz,tl,tr,val); else { Update(lz,tl,mid,val); Update(rz,mid+1,tr,val); } push_up(u);}__int64 Query(int u, int l, int r, int tl, int tr){ if(tl<=l&&r<=tr) { return tree[u]; } push_down(u,l,r); int mid=(l+r)>>1; if(tr<=mid) return Query(lz,tl,tr); else if(tl>mid) return Query(rz,tl,tr); else { __int64 t1=Query(lz,tl,mid); __int64 t2=Query(rz,mid+1,tr); return t1+t2; } push_up(u);}int main(){ int n, m, l, r; while(cin >> n >> m) { for(int i=1; i<=n; i++) scanf("%I64d",a+i); build(1,1,n); while(m--) { char ch[5]; __int64 val; scanf("%s",ch); if(ch[0]=='C') { scanf("%d%d%I64d",&l,&r,&val); Update(1,1,n,l,r,val); } else { scanf("%d%d",&l,&r); printf("%I64d\n",Query(1,1,n,l,r)); } } } return 0;}
0 0
- 成段加值更新 区间求和 poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和
- poj3468 A Simple Problem with Integers 线段树延迟标记区间更新区间求和
- poj3468 A Simple Problem with Integers 线段树区间更新,区间求和
- POJ3468 A simple problem with integers(区间更新)
- poj3468 A Simple Problem with Integers 线段树区间更新
- Poj3468 A Simple Problem with Integers 线段树、区间更新
- POJ3468 A Simple Problem with Integers(区间更新)
- (poj3468)A Simple Problem with Integers(区间更新)
- POJ3468 A Simple Problem with Integers 区间更新
- 【poj3468-A Simple Problem with Integers】-线段树区间更新
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- poj3468 A Simple Problem with Integers 线段树区间累加求和
- poj3468 A Simple Problem with Integers(成段增减 区间求和)
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
- POJ3468 A Simple Problem with Integers 线段树|树状数组BIT(区间增减,求和)
- POJ3468——A Simple Problem with Integers(线段树区间求和加减)
- 线段树 区间更新 区间查询 poj3468 A Simple Problem with Integers
- 收藏一个SharedPreferences工具类
- WCF学习(一)_Web Service+SOA+WCF简介
- 分水岭算法MATLAB编程代码解析
- 程序员书单_软考篇
- GNU链接脚本学习(1)
- 成段加值更新 区间求和 poj3468 A Simple Problem with Integers
- css 填坑常用代码分享
- 黑马程序员——Collection单列集合
- MySQL锁;Lock wait timeout exceeded; try restarting transaction
- UIScrollView属性
- jQuery源码分析之jQuery中常用正则表达式分析
- ZOJ 3897 Fiddlesticks(模拟题)
- sg函数学习小结
- 程序员书单_程序人生篇