poj 3468 A Simple Problem with Integers(线段树区间更新)
来源:互联网 发布:阿里云服务器如何退款 编辑:程序博客网 时间:2024/05/16 09:18
题意:一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。
分析:线保留型线段树,线段树中每个节点有两个变量:增量与和,一个记录当前节点对应区间被整体增加了几,另一个记录该区间的真子区间被增加了之后的和是多少。(该区间数字当前和=和+增量×区间长度)
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <cmath>#include <vector>#include <set>#include <list>#include <queue>#include <map>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF 0xFFFFFFFF#define pi acos(-1.0)#define eps 1e-4#define maxn 100010#define MOD 1000000007struct node{ int l,r; long long sum,lazy;}tree[maxn*4];void pushup(int pos){ tree[pos].sum = tree[pos<<1].sum + tree[pos<<1|1].sum;}void pushdown(int pos){ if(!tree[pos].lazy) return; tree[pos<<1].sum += (tree[pos<<1].r-tree[pos<<1].l+1)*tree[pos].lazy; tree[pos<<1|1].sum += (tree[pos<<1|1].r-tree[pos<<1|1].l+1)*tree[pos].lazy; tree[pos<<1].lazy += tree[pos].lazy; tree[pos<<1|1].lazy += tree[pos].lazy; tree[pos].lazy = 0;}void build(int l,int r,int pos){ tree[pos].l = l; tree[pos].r = r; tree[pos].sum = 0; tree[pos].lazy = 0; if(l == r) { scanf("%I64d",&tree[pos].sum); return; } int mid = (l+r)/2; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos);} //建树 //查询操作void update(int l,int r,int pos,int add){ if(tree[pos].l == l && tree[pos].r == r) { tree[pos].sum += (r-l+1)*add; tree[pos].lazy += add; return ; } pushdown(pos); int mid = (tree[pos].l + tree[pos].r) >> 1; if(r <= mid) update(l,r,pos<<1,add); else if(l > mid) update(l,r,pos<<1|1,add); else { update(l,mid,pos<<1,add); update(mid+1,r,pos<<1|1,add); } pushup(pos);} //自上而下更新节点long long query(int l,int r,int pos){ if(l == tree[pos].l && r == tree[pos].r) return tree[pos].sum; pushdown(pos); int mid = (tree[pos].l+tree[pos].r)>>1; if(r <= mid) return query(l,r,pos<<1); else if(l > mid) return query(l,r,pos<<1|1); else return query(l,mid,pos<<1) + query(mid+1,r,pos<<1|1);}int main(){ int t,n,m,C = 1; //scanf("%d",&t); while(scanf("%d%d",&n,&m) != EOF) { int a,b,c; char ch; build(1,n,1); for(int i = 1; i <= m; i++) { getchar(); scanf("%c%d%d",&ch,&a,&b); if(ch == 'Q') printf("%I64d\n",query(a,b,1)); else { scanf("%d",&c); update(a,b,1,c); } } } return 0;}
0 0
- A Simple Problem with Integers +poj+线段树区间更新
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 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(线段树(区间更新))
- POJ 3468 A Simple Problem with Integers(线段树区间更新+求和)
- MFC对单文档进行窗口分割。
- [Android小技巧]如何不用图片创建一个带圆角的按钮
- iOS 25个性能优化/内存优化常用方法
- Unity 实现倒计时 协程
- Runtime Method Swizzling
- poj 3468 A Simple Problem with Integers(线段树区间更新)
- curl http_code 状态码 意义及信息
- java异常处理机制
- Nagios 监控系统架设全攻略
- Java基础07 包
- Java基础:Day14笔记内容 ( StringBuilder类)
- SQL Server 视图创建点滴
- 神奇的 collectionview
- 主线程等待子线程运行完之后再运行