线段树的lazy(poj3468)

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 73163   Accepted: 22585
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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.

Source

 
  最裸的线段树lazy标记
 
 #include <iostream>
#include <cstdio>
using namespace std;
const int N = ;
typedef long long LL;
LL sum[N<<]; //sum用来存储每个节点的子节点数值的总和
LL add[N<<];//add用来记录该节点的每个数值应该加多少
struct Node{
int l,r;//表示改点的左右区间
int mid(){//结构体函数
return (l+r)>>;
}
} tree[N<<]; void PushUp(int rt){//算某一节点的左右孩子值的和
sum[rt] = sum[rt<<] + sum[rt<<|];
} void PushDown(int rt,int m){//rt当前节点 m此节点的区间长度
if(add[rt]!=){//如果当前节点lazy标记不为 0
add[rt<<] += add[rt];//左右孩子lazy累加
add[rt<<|] += add[rt];
sum[rt<<] += add[rt]*(m-(m>>));//更新计算左右孩子
sum[rt<<|] += add[rt] * (m>>);
add[rt] = ;//小细节,但很重要,不能忘记lazy用过后清零
}
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
add[rt] = ;//lazy标记记为0
if(l == r){
scanf("%lld",&sum[rt]);//把子节点信息记录在sum里
return ;
}
int m = tree[rt].mid();
build(l,m,rt<<);//建立左右孩子,这时编号是按照类似“宽搜”来编的
build(m+,r,rt<<|); PushUp(rt);//算出此节点的权值
} void update(int c,int l,int r,int rt){//当前节点rt,在l和r区间上加上c
if(l<=tree[rt].l&&tree[rt].r<=r){//当前节点所表示的区间完全被所要更新的区间包含
add[rt]+=c;//lazy累加,add[i]数组是针对i左右孩子的
sum[rt]+=(LL)c*(tree[rt].r-tree[rt].l+);//这句话很重要和下面的PushUP()类似
return;
}
PushDown(rt,tree[rt].r - tree[rt].l + );//用rt的lazy更新其子节点
int m = tree[rt].mid();
if(l<=m) update(c,l,r,rt<<);
if(m+<=r) update(c,l,r,rt<<|);
PushUp(rt);
} LL query(int l,int r,int rt){//当前节点为rt,求l,到r的和
if(l<=tree[rt].l&&tree[rt].r<=r){//区间完全包含,可以直接返回
return sum[rt];
}
//因为此时用到rt节点,所以才更新lazy
PushDown(rt,tree[rt].r-tree[rt].l + ); int m = tree[rt].mid();
LL res = ; if(l<= m)//要求的区间有一部分在mid的左边
res += query(l,r,rt<<);
if(m+<=r)
res += query(l,r,rt<<|);
return res;
} int main(){ int n,m; scanf("%d %d",&n,&m);
build(,n,); while(m--){
char ch[];
scanf("%s",ch);
int a,b,c;
if(ch[] == 'Q'){
scanf("%d %d", &a,&b);
printf("%lld\n",query(a,b,));
}
else{
scanf("%d %d %d",&a,&b,&c);
cin>>a>>b>>c;
update(c,a,b,);
}
} return ;
}

还有一种较好理解的方法:

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
inline LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const LL maxn=;
struct node{
LL num;
LL l,r;
LL lc,rc;
LL sum,lazy;
}seg[maxn*];
LL a[maxn];
LL N,M,tot=;
inline void updata_son(LL root){
LL d=seg[root].lazy;
if(d!=){
LL lson=seg[root].lc; LL rson=seg[root].rc;
seg[lson].lazy+=d; seg[rson].lazy+=d; seg[root].lazy=;
seg[lson].sum+=d*(seg[lson].r-seg[lson].l+);
seg[rson].sum+=d*(seg[rson].r-seg[rson].l+); }
}
inline void Build(LL root,LL l,LL r){
seg[root].num=root;
seg[root].l=l; seg[root].r=r;
if(l==r){
seg[root].sum=a[l];
return ;
}
LL mid=(l+r)>>;
seg[root].lc=++tot; Build(tot,l,mid);
seg[root].rc=++tot; Build(tot,mid+,r);
seg[root].sum=seg[seg[root].lc].sum+seg[seg[root].rc].sum;
}
inline LL query(LL root,LL l,LL r){
if(l<=seg[root].l&&seg[root].r<=r){
return seg[root].sum;
}
updata_son(root);
LL ans=;
LL mid=(seg[root].l+seg[root].r)>>;
if(l<=mid) ans+=query(seg[root].lc,l,r);
if(mid+<=r) ans+=query(seg[root].rc,l,r);
return ans;
} inline void change(LL root,LL l,LL r,LL delta){
if(l<=seg[root].l&&seg[root].r<=r){
seg[root].sum+=(seg[root].r-seg[root].l+)*delta;
seg[root].lazy+=delta;
return ;
}
updata_son(root);
LL mid=(seg[root].l+seg[root].r)>>;
if(l<=mid) change(seg[root].lc,l,r,delta);
if(mid+<=r) change(seg[root].rc,l,r,delta);
seg[root].sum=seg[seg[root].lc].sum+seg[seg[root].rc].sum;
}
int main(){
N=read(); M=read();
for(LL i=;i<=N;i++) a[i]=read();
Build(,,N);
LL ll,rr,de;
char x[];
for(LL i=;i<=M;i++){
scanf("%s",x);
if(x[]=='C'){
ll=read(); rr=read(); de=read();
change(,ll,rr,de);
continue;
}
if(x[]=='Q'){
ll=read(); rr=read();
printf("%lld\n",query(,ll,rr));
}
}
return ;
}
上一篇:浅谈算法——线段树之Lazy标记


下一篇:Mayor's posters (离散化线段树+对lazy的理解)