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(poj3468)的更多相关文章

  1. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  2. 线段树入门&lazy思想

    线段树将区间分成若干个子区间,子区间又继续分,直到区间为一个点(区间左值等于右值) 对于父区间[a,b],其子区间为[a,(a+b)/2]和[(a+b)/2+1,b] 用于求区间的值,如区间最值.区间 ...

  3. 浅谈算法——线段树之Lazy标记

    一.前言 前面我们已经知道线段树能够进行单点修改和区间查询操作(基本线段树).那么如果需要修改的是一个区间该怎么办呢?如果是暴力修改到叶子节点,复杂度即为\(O(nlog n)\),显然是十分不优秀的 ...

  4. Mayor's posters (离散化线段树+对lazy的理解)

    题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...

  5. 线段树初步&&lazy标记

    线段树 一.概述: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a, ...

  6. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  7. 线段树(lazy)-hdu1689

    题目链接:https://vjudge.net/problem/HDU-1698 题目描述: 现在Pudge想做一些操作.让我们将钩子的连续金属棒从1编号到N.对于每个操作,Pudge可以将连续金属棒 ...

  8. 线段树(lazy标记)-- 模板

    ], lazy[MAXN << ]; void PushUp(int rt) { ans[rt] = ans[rt << ] + ans[rt << | ]; } ...

  9. HihoCoder1080 更为复杂的买卖房屋姿势(线段树+多重lazy)

    描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们可以化身上帝模式,买卖房产. 在这个游戏里,会不断的发生如下两种事件:一种是房屋自发的涨价或者降价,而另一种是政 ...

随机推荐

  1. Laravel5.1 模型 --多态关联

    什么是多态关联? 一个例子你就明白了:好比如说评论 它可以属于视频类 也可以属于文章类,当有个需求是 从评论表中取到视频类的数据,这就需要用到多态关联了. 简单的一句话总结:一张表对应两张表. 1 实 ...

  2. leveldb学习笔记

    LevelDB由 Jeff Dean和Sanjay Ghemawat开发. LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库. 特别如下: 1.LevelDb是一 ...

  3. Android实例-退出程序

    Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...

  4. 编写高质量代码–改善python程序的建议(二)

    原文发表在我的博客主页,转载请注明出处! 建议七:利用assert语句来发现问题断言(assert)在很多语言中都存在,它主要为调试程序服务,能够快速方便地检查程序的异常或者发现不恰当的输入等,可防止 ...

  5. 02、微信小程序的数据绑定

    02.微信小程序的数据绑定 目录结构: 模板内容: 使用bindtap绑定事件 <!--index.wxml--> <view class="container" ...

  6. 170214、mybatis一级和二级缓存

    mybatis一级缓存是指在内存中开辟一块区域,用来保存用户对数据库的操作信息(sql)和数据库返回的数据,如果下一次用户再执行相同的请求, 那么直接从内存中读数数据而不是从数据库读取. 其中数据的生 ...

  7. Java基础语法 - 面向对象 - 局部变量

    如果在一个成员方法内定义一个变量,那么这个变量就被称为局部变量. 局部变量在方法执行时被创建,在方法执行结束时被销毁.局部变量在使用时必须进行赋值操作或被初始化,否则会出现编译错误 package m ...

  8. List 接口常用子类及其特点

    List 常用子类: - Vector: 内部是数组数据结构,是同步的. 增删, 查询都很慢 - ArrayList: 内部是数组数据结构,是不同步的,替代了 Vector,不同步的效率较高. 特点: ...

  9. python基础之类的继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法

    一.什么是继承 继承是一种创建新的类的方式,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. 派生:子类继承了父类的属性,然后衍生出自己新的属性,如果子类衍生出的新 ...

  10. Python 一键同步windows和linux数据(基于pscp.exe)

    outline 项目中需要把 windows server 上的数据同步到 linux server,方法很多,这里记录下自己采用的一种比较简单的方法. 准备工作 首先确保你 windows serv ...