http://poj.org/problem?id=3468

题意:

给出一串数,每次在一个区间内增加c,查询[a,b]时输出a、b之间的总和。

思路:

总结一下懒惰标记的用法吧。

比如要对一个区间范围内的数都要加c,在找到这个区间之后,本来它的孩子结点也是需要更新的,但是我们可以暂时不更新,如果到时候需要用到这些孩子结点的时候,我们再来更新。这个时候就要用到懒惰标记了,也就是add[o]=c,之后它的孩子结点更新时就只需要加上add[o]就可以了。

 #include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn = + ;
int n, m; long long add[maxn << ];
long long sum[maxn << ]; void PushDown(int o, int m)
{
if (add[o])
{
//传递懒惰标记
add[o << ] += add[o];
add[o << | ] += add[o];
//更新子节点的值
sum[o << ] += add[o] * (m - (m >> ));
sum[o << | ] += add[o] * (m >> );
//出去懒惰标记
add[o] = ;
}
} void PushUp(int o)
{
sum[o] = sum[o << ] + sum[o << | ];
} void build(int L, int R, int o)
{
add[o] = ;
if (L == R)
{
scanf("%lld", &sum[o]);
return;
}
int mid = (L + R) / ;
build(L, mid, * o);
build(mid + , R, * o + );
PushUp(o);
} void update(int L, int R, int x, int l,int r,int o)
{
if (L <= l && R >= r) //如果找到区间了,则不需要往下更新孩子结点了,等下次需要时再更新
{
add[o] += x;
sum[o] += (r - l + )*x;
return;
}
PushDown(o, r - l + );
int mid = (l + r) / ;
if (L <= mid)
update(L, R, x, l, mid, * o);
if (R > mid)
update(L, R, x, mid + , r, * o + );
PushUp(o);
} long long query(int L, int R, int l, int r, int o)
{
if (L <= l && R >= r)
return sum[o];
PushDown(o, r - l + );
int mid = (l + r) / ;
long long ans = ;
if (L <= mid)
ans += query(L, R, l, mid, * o);
if (R > mid)
ans += query(L, R, mid + , r, * o + );
return ans;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d%d", &n, &m))
{
build(, n, );
char c[];
int x, y, z;
while (m--)
{
scanf("%s", &c);
if (c[] == 'Q')
{
scanf("%d%d", &x, &y);
printf("%lld\n", query(x, y, , n, ));
}
else
{
scanf("%d%d%d", &x, &y, &z);
update(x, y, z, , n, );
}
}
}
}

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(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

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

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

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

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #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 ...

  10. 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. for update 和 t.rowid的区别

    select * from table_name for update; 和 select t.*, t.rowid from table_name t 的区别 前者会对你查询出来的结果加上锁,而后者 ...

  2. java设计模式----迭代子模式

    顺序访问聚集中的对象,主要用于集合中.一是需要遍历的对象,即聚集对象,二是迭代器对象,用于对聚集对象进行遍历访问. 迭代子模式为遍历集合提供了统一的接口方法.从而使得客户端不需要知道聚集的内部结构就能 ...

  3. windows MySQL5.7.9免安装版配置方法

    1. 解压MySQL压缩包    将下载的MySQL压缩包解压到自定义目录下,我的解压目录是:    "D:\Program Files\mysql-5.7.9-win32"    ...

  4. angular -- post请求该如何使用?

    angularjs 的post 请求该如何调用? 简单示例: // post 携带参数访问 $http({ method:'post', url:postUrl, data:{name:"a ...

  5. 百度地图API开发----手机地图做导航功能

    第一种方式:手机网页点击打开直接进百度地图APP <a href="baidumap://map/direction?mode=[transit:公交,driving:驾车]& ...

  6. Http协议中Cookie详细介绍(转)

    原文:http://www.169it.com/article/3217120921.html Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存C ...

  7. 剑指offer总结

    1.实现Singleton模式 2.二维数组中的查找:每行从左到右递增,每列从上到下递增,输入一个数,判断数组中是否存在该数 1 2  8 9  2 4  9 12 4 7 10 13  6 8 11 ...

  8. 微软官方推出的win10安装或者创建安装u盘的工具

    https://www.microsoft.com/zh-cn/software-download/windows10 下载安装后,可根据提示,一步步的安装win10或者创建安装u盘

  9. Python之迭代器及生成器

    一. 迭代器 1.1 什么是可迭代对象 字符串.列表.元组.字典.集合 都可以被for循环,说明他们都是可迭代的. 我们怎么来证明这一点呢? from collections import Itera ...

  10. IETF国标查询

    IETF官网 https://www.ietf.org/ rfc国标官网 https://www.ietf.org/standards/rfcs/ rfc国标查询 https://www.rfc-ed ...