poj 3468 A Simple Problem with Integers(线段树——区间更新)
2017-04-20 21:41
197 查看
A Simple Problem with Integers
DescriptionYou 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
ps:裸的区间更新模板题,lazy思想(延迟更新)真的是lazy啊,如果不用那一段区间时就不会更新它的值,什么时候用什么时候才会更新
代码:
#include<stdio.h> #define maxn 100010 typedef __int64 LL; struct node { LL val,le,ri; LL mid() { return (le+ri)>>1; } } tree[maxn*4]; LL a[maxn],add[maxn*4]; void Build(LL rt,LL left,LL right) { tree[rt].le=left; tree[rt].ri=right; if(left==right) { tree[rt].val=a[left]; return ; } LL mid=tree[rt].mid(); Build(rt<<1,left,mid); Build(rt<<1|1,mid+1,right); tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val; } void Pushdown(LL rt,LL m) { if(add[rt]) { add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt]; tree[rt<<1].val+=add[rt]*(m-(m>>1));//注意这里,想一下 tree[rt<<1|1].val+=add[rt]*(m>>1);//还有这里 add[rt]=0; } } void Updata(LL rt,LL left,LL right,LL num) { if(tree[rt].le==left&&right==tree[rt].ri) { add[rt]+=num; tree[rt].val+=num*(tree[rt].ri-tree[rt].le+1); return ; } Pushdown(rt,tree[rt].ri-tree[rt].le+1); LL mid=tree[rt].mid(); if(right<=mid) Updata(rt<<1,left,right,num); else if(left>mid) Updata(rt<<1|1,left,right,num); else { Updata(rt<<1,left,mid,num); Updata(rt<<1|1,mid+1,right,num); } tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val; } LL Query(LL rt,LL left,LL right) { if(tree[rt].le==left&&tree[rt].ri==right) return tree[rt].val; Pushdown(rt,tree[rt].ri-tree[rt].le+1); LL mid=tree[rt].mid(); if(right<=mid) return Query(rt<<1,left,right); else if(left>mid) return Query(rt<<1|1,left,right); else return Query(rt<<1,left,mid)+Query(rt<<1|1,mid+1,right); } int main() { LL n,q,x,y,z; char s[5]; scanf("%I64d%I64d",&n,&q); for(LL i=1; i<=n; ++i) scanf("%I64d",&a[i]); Build(1,1,n); for(LL i=1; i<=q; ++i) { scanf("%s",s); if(s[0]=='Q') { scanf("%I64d%I64d",&x,&y); printf("%I64d\n",Query(1,x,y)); } else { scanf("%I64d%I64d%I64d",&x,&y,&z); Updata(1,x,y,z); } } 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 【线段树 + 区间更新lazy】
- 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 区间add更新
- 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(线段树区间更新 or 树状数组区间更新)
- (简单) 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(线段树_区间更新+lazy标记)
- POJ 3468 A Simple Problem with Integers(线段树 区间更新)
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- POJ-3468-A Simple Problem with Integers(线段树区间更新)