A Simple Problem with Integers(线段树+区间修改+区间询问模板)
来源:互联网 发布:一键php加mysql环境 编辑:程序博客网 时间:2024/06/17 19:08
(POJ - 3468)A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 119060 Accepted: 36974
Case Time Limit: 2000MS
Description
You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
The sums may exceed the range of 32-bit integers.
题目大意:一个有n个数的序列支持两种操作:C:对区间[l,r]中的每一个数都加一个值val;Q:询问区间[l,r]的和。
思路:线段树模板。区间修改,区间询问。
代码:
#include<cstdio>#define ls o<<1#define lr o<<1|1using namespace std;typedef long long LL;const int maxn=100005;LL a[maxn];int n,m;struct node{ int l,r; LL sum; LL lazy; int len;}T[maxn<<2];void pushup(int o){ T[o].sum=T[ls].sum+T[lr].sum;}void build(int o,int l,int r){ T[o].l=l; T[o].r=r; T[o].lazy=0;//设置延迟标记 T[o].len=r-l+1; if(l==r)//叶子节点 { T[o].sum=a[l]; return; } int mid=(l+r)>>1; build(ls,l,mid);//递归构造左子树 build(lr,mid+1,r);//递归构造右子树 pushup(o);//根据左右子树节点的值向上更新当前结点的值 }/*当前结点的标志向孩子结点传递*/ void pushdown(int o){ T[ls].sum+=T[o].lazy*T[ls].len; T[lr].sum+=T[o].lazy*T[lr].len; T[ls].lazy+=T[o].lazy; T[lr].lazy+=T[o].lazy; T[o].lazy=0;//传递后,当前结点标记清空 }void update(int o,int l,int r,int lazy){ if(T[o].l==l&&T[o].r==r) { T[o].sum+=T[o].len*lazy; T[o].lazy+=lazy; return; } if(T[o].lazy) pushdown(o); int mid=(T[o].l+T[o].r)>>1; if(mid>=r) update(ls,l,r,lazy); else if(mid<l) update(lr,l,r,lazy); else { update(ls,l,mid,lazy); update(lr,mid+1,r,lazy); } //根据左右子树的值回溯向上更新当前结点的值 pushup(o);}LL query(int o,int l,int r){ if(T[o].l==l&&T[o].r==r) return T[o].sum; if(T[o].lazy) pushdown(o); int mid=(T[o].l+T[o].r)>>1; if(mid>=r) return query(ls,l,r); else if(mid<l) return query(lr,l,r); else return query(ls,l,mid)+query(lr,mid+1,r);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%lld",a+i); build(1,1,n); while(m--) { char op[3]; scanf("%s",op); if(op[0]=='C') { int l,r,v; scanf("%d%d%d",&l,&r,&v); update(1,l,r,v); } else { int l,r; scanf("%d%d",&l,&r); printf("%lld\n",query(1,l,r)); } } return 0;}
- A Simple Problem with Integers(线段树+区间修改+区间询问模板)
- POJ_3468 A Simple Problem with Integers(线段树区间修改+附线段树模板)
- POJ A Simple Problem with Integers (线段树区间更新区间查询模板)
- POJ-3468-A Simple Problem with Integers(线段树区间修改+区间求和)
- poj 3468 A Simple Problem with Integers(线段树区间修改+区间求和)
- A Simple Problem with Integers(线段树,区间求和)
- 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(线段树区间更新模板题)
- poj3468 A Simple Problem with Integers(线段树+区间更新+非完全替换)模板
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- poj3468 A Simple Problem with Integers(zkw区间修改模板)
- poj3468 A Simple Problem with Integers(线段树区间修改)
- poj 3468 A Simple Problem with Integers(线段树区间修改)
- POJ 3468 A Simple Problem with Integers(线段树区间修改)
- yum安装nginx
- Android Launcher 启动 Activity 的工作过程
- 51单片机外设之——按键的检测(带有标志位的按键识别法)
- AtomicBoolean介绍与使用
- js-es6-数组扩展
- A Simple Problem with Integers(线段树+区间修改+区间询问模板)
- 单链表
- 10.3 扯淡
- HDU 5120 (计算几何+圆相交)
- codeforces 712D Memory and Scores (dp)
- 电路交换与分组交换的区别
- IoC/DI理解
- Update 错数据恢复
- AtCoder 2370 Piling