A Simple Problem with Integers 【线段树】-区间加减求和
来源:互联网 发布:115年费会员淘宝 编辑:程序博客网 时间:2024/05/21 14:54
A Simple Problem with Integers
给出了一个序列,你需要处理如下两种询问。
“C a b c”表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
“Q a b” 询问[a, b]区间中所有值的和。
Input
第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。
接下来Q行询问,格式如题目描述。
Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。
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
思路 模版题
代码
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define M 5000000#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;struct Tree{ LL l,r; LL len; LL sum; LL lazy;}tree[M<<2];void pushup(LL o){ tree[o].sum=tree[rr].sum+tree[ll].sum;}void pushdown(LL o){ if(tree[o].lazy) { tree[ll].lazy+=tree[o].lazy; tree[rr].lazy+=tree[o].lazy; tree[rr].sum+=tree[o].lazy*tree[rr].len; tree[ll].sum+=tree[o].lazy*tree[ll].len; tree[o].lazy=0; }}void build(LL o,LL l,LL r){ tree[o].l=l;tree[o].r=r; tree[o].len=r-l+1; tree[o].lazy=0; if(r==l) { LL a; scanf("%lld",&a); tree[o].sum=a; return; } int mid=( l+r )>>1; build(lson); build(rson); pushup(o);}void update(LL o,LL l,LL r,LL val){ if(l<=tree[o].l&&tree[o].r<=r) { tree[o].lazy+=val; tree[o].sum+=val*tree[o].len; return; } pushdown(o); int mid=(tree[o].l+tree[o].r)>>1; if(r<=mid) update(ll,l,r,val); else if(l>mid) update(rr,l,r,val); else { update(ll,l,mid,val); update(rr,mid+1,r,val); } pushup(o);}LL query(LL o,LL l,LL r){ if(tree[o].l>=l&&tree[o].r<=r) return tree[o].sum; pushdown(o); int mid=(tree[o].l+tree[o].r)>>1; if(r<=mid) query(ll,l,r); else if(l>mid) query(rr,l,r); else return query(ll,l,mid)+query(rr,mid+1,r);}int main(){ LL n,q; while(~scanf("%lld%lld",&n,&q)) { build(1,1,n); while(q--) { char c[2]; scanf("%s",c); if(c[0]=='Q') { LL x,y; scanf("%lld%lld",&x,&y); printf("%lld\n",query(1,x,y)); } else { LL a,b,c; scanf("%lld%lld%lld",&a,&b,&c); update(1,a,b,c); } } } return 0;}
0 0
- 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(线段树区间求和加减)
- A Simple Problem with Integers(线段树,区间求和)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)
- 线段树系列-pku-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 线段树区间更新,区间求和
- POJ 3468 A Simple Problem with Integers 数据结构+线段树+区间更新+区间求和
- POJ-3468-A Simple Problem with Integers(线段树区间修改+区间求和)
- A Simple Problem with Integers (线段树,区间求和+区间更新)
- poj 3468 A Simple Problem with Integers(线段树区间修改+区间求和)
- 第五章 JAVA数组初级学习
- 模拟实现queue
- static执行顺序
- 51nod 1103 N的倍数 (抽屉原理)
- HDU 4454 Stealing a Cake (三分)
- A Simple Problem with Integers 【线段树】-区间加减求和
- Java 8 中的 Streams API 详解
- JS逻辑运算题之switch的使用
- String s=new String("abc")创建了几个对象?
- Android表格布局TableLayout简单实现(Java动态添加,设置边框,删除数据(单行,多行))
- Linux下的fdisk用法
- mybatis generator的使用
- 51nod 1460 连接小岛 (贪心)
- 面向对象高级( 随手记-7)