题目地址:POJ 3468

打了个篮球回来果然神经有点冲动。

无脑的狂交了8次WA。。竟然是更新的时候把r-l写成了l-r。。。

这题就是区间更新裸题。

区间更新就是加一个lazy标记,延迟标记,仅仅有向下查询的时候才将lazy标记向下更新。其它的均按线段树的来即可。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <stack>
using namespace std;
#define LL long long
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int MAXN=1e5+10;
LL sum[MAXN<<2];
LL lazy[MAXN<<2];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt, int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l, int r, int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int ll, int rr, int x, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
lazy[rt]+=x;
sum[rt]+=(LL)x*(r-l+1);
return ;
}
PushDown(rt, r-l+1);
int mid=(l+r)>>1;
if(ll<=mid) update(ll,rr,x,lson);
if(rr>mid) update(ll,rr,x,rson);
PushUp(rt);
}
LL query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt,r-l+1);
LL ans=0;
int mid=(l+r)>>1;
if(ll<=mid) ans+=query(ll,rr,lson);
if(rr>mid) ans+=query(ll,rr,rson);
return ans;
}
int main()
{
int n, q, i, a, b, c;
char ch[3];
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
getchar();
scanf("%s",&ch);
if(ch[0]=='Q')
{
scanf("%d%d",&a,&b);
LL ans=query(a,b,1,n,1);
printf("%lld\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
return 0;
}

POJ 3468 A Simple Problem with Integers(线段树区间更新)的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  7. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. 预测学习、深度生成式模型、DcGAN、应用案例、相关paper

    我对GAN"生成对抗网络"(Generative Adversarial Networks)的看法: 前几天在公开课听了新加坡国立大学[机器学习与视觉实验室]负责人冯佳时博士在[硬 ...

  2. 2-2 列表推导同 filter 和 map 的比较

    列表推导同 filter 和 map 的比较 参考廖雪峰的文档: filter()函数:用于过滤序列. filter()接收一个函数和一个序列.把传入的函数依次作用于传入的序列的每个元素,根据返回值是 ...

  3. Windows提高_2.1第一部分:线程

    第一部分:线程 什么是线程? 线程其实可以理解为一段正在执行中的代码,它最少由一个线程内核对象和一个栈组成. 线程之间是没有从属关系的,同一进程下的所有线程都可以访问进程内的所有内容. 主线程其实是创 ...

  4. radiobutton group

    1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置  radio1  的 属性:  group.tabstop ...

  5. 一天搞定jQuery(三)——使用jQuery完成复选框的全选和全不选

    还记得之前我使用JavaScript来实现复选框的全选和全不选效果吗?如果读者初次翻阅本文,可记得看看教你一天玩转JavaScript(七)——使用JavaScript完成复选框的全选和全不选的效果! ...

  6. docker 转载

    写的非常好的一篇文章,不知道为什么被删除了.  利用Google快照,做个存档. 快照地址:地址 作者地址:青牛 什么是docker Docker 是一个开源项目,诞生于 2013 年初,最初是 do ...

  7. contab的使用方法

    linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...

  8. ubuntu下手动配置apache2.4.12

    (apache2也可以使用 sudo apt-get install apache2来安装,下面来讲解下如何手动安装配置apache2) 在安装apache2之前,先要安装apache2的依赖项,ap ...

  9. 在mac上面运行cherrytree

    下载源码包 wget http://www.giuspen.com/software/cherrytree-0.38.4.tar.xz 解压 tar -xvf cherrytree-0.38.4.ta ...

  10. Non-inclusive cache method using pipelined snoop bus

    A non-inclusive cache system includes an external cache and a plurality of on-chip caches each havin ...