题目连接:http://poj.org/problem?id=3468

线段树功能:update:成段增减 query:区间求和。

分析:需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define M 1000000000
#define maxn 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
LL sum[maxn<<];
LL col[maxn<<];
void Pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void Pushdown(int rt,int m)
{
if(col[rt])
{
col[rt<<]+=col[rt];
col[rt<<|]+=col[rt];
sum[rt<<]+=col[rt]*(m-(m>>));
sum[rt<<|]+=col[rt]*(m>>);
col[rt]=;
}
}
void build(int l,int r,int rt)
{
col[rt]=;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]+=c;
sum[rt]+=(LL)(r-l+)*c;
return;
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update(L,R,c,lson);
if(m<R)update(L,R,c,rson);
Pushup(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
Pushdown(rt,r-l+);
int m=(r+l)>>;
LL res=;
if(L<=m)res+=query(L,R,lson);
if(R>m)res+=query(L,R,rson);
return res;
}
int main()
{
int n,m;
char op[];
while(scanf("%d%d",&n,&m)>)
{
build(,n,);
while(m--)
{
int a,b,c;
scanf("%s",op);
if(op[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,,n,));
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,n,);
}
}
}
return ;
}

poj3468(线段树)的更多相关文章

  1. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

  2. POJ3468 线段树(区间更新,区间求和,延迟标记)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97196   ...

  3. poj3468 线段树+lazy标记

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92921   ...

  4. POJ3468(线段树 区间修改 lazy-tag)

    我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...

  5. POJ3468(线段树区间维护)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 85502   ...

  6. poj3468(线段树区间更新&区间求和模板)

    题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

  7. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  8. poj3468(线段树 边覆盖)

    #include<cstdio> int lb,rb,data; long long sum[5000000],extra[5000000]; void add(int l,int r,i ...

  9. poj3468线段树标记永久化

    #include<map> #include<set> #include<list> #include<cmath> #include<queue ...

  10. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之1091多项式

        题目 解决代码及点评 /************************************************************************/ /* ...

  2. VPS服务器下的centos网卡配置详解……

    自动激活网卡 安装了CENTOS 6.X后,每次启动了系统都需要手动激话网卡,以下方法可以在系统启动后自动激活网卡. cat /etc/sysconfig/network-scripts/ifcfg- ...

  3. php 实时汇率接口

    ​function getExchangeRate($from_Currency,$to_Currency) { $amount = urlencode($amount); $from_Currenc ...

  4. Oracle判断指定列是否全部为数字

    select nvl2(translate(name,'\1234567890 ', '\'),'is   characters ','is   number ') from customer_inf ...

  5. 【app】遍历目录所有文件

    遍历目录所有文件   原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...

  6. 开始翻译Windows Phone 8 Development for Absolute Beginners教程

    Bob Tabor (LearnVisualStudio.NET)和Clint Rutkas (Microsoft/Channel9)合作推出了超过11小时的针对初学者的Windows Phone 8 ...

  7. 一起来开发Android的天气软件(三)——使用Volley实现网络通信

    距离上一篇一起来开发Android天气软件二的时间又将近半个月了,之间一直由于有事而没有更新实在抱歉,近期会加快更新的步伐.争取在2015年到来前写完这系列的博文,上一章我们已经使用LitePal框架 ...

  8. oracle RAC搭建中的潜规则 该死的app

    oracle RAC 安装目录必须是 ORACLE_BASE=/u01/app/grid ORACLE_HOME=/u01/app/11.2.0/grid 看着中间有个app,又没什么实际用处,就删掉 ...

  9. BZOJ 1212 HNOI 2004 L语言 Trie树

    标题效果:给一些词.和几个句子,当且仅当句子可以切子可以翻译词典,这意味着该子将被翻译. 找到最长前缀长度可以被翻译. 思维:使用Trie树阵刷.你可以刷到最长的地方是最长的字符串可以翻译到的地方. ...

  10. Swift - 类和结构体的区别

    类和结构体有许多相同之处,也有许多不同之处. 二者区别如下: 1,类可以继承和扩展,结构体不可以 2,可以让一个类的实例来反初始化,释放存储空间,结构体做不到 3,类的对象是引用类型,而结构体是值类型 ...