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. C#IIS网站应用程序池启动回收停止 .

    //添加应用程序池空间引用using System.DirectoryServices;using System.Text; using System.Text.RegularExpressions; ...

  2. nginx proxy模块

    环境: user:192.168.100.169 nginx代理:192.168.100.175 tomcat:192.168.100.175 域名:www.vijay.com  --->192 ...

  3. C++ list容器系列功能函数详解

    C++ list函数详解 首先说下eclipse工具下怎样debug:方法:你先要设置好断点,然后以Debug方式启动你的应用程序,不要用run的方式,当程序运行到你的断点位置时就会停住,也会提示你进 ...

  4. Android--aapt命令

    1.aapt l[ist] [-v] [-a] file.{zip,jar,apk} 释义:列出压缩文件中的内容 aapt l xxx.apk:简单的罗列压缩文件中每一项的内容 aapt l -v x ...

  5. Educational Codeforces Round 28

    A. Curriculum Vitae 题目链接:http://codeforces.com/contest/846/problem/A 题目意思:给你一个只包含0-1的数组,现在要求去可以去掉一些元 ...

  6. day18(JDBC事务&连接池介绍&DBUtils工具介绍&BaseServlet作用)

    day18总结 今日思维导图: 今日内容 事务 连接池 ThreadLocal BaseServlet自定义Servlet父类(只要求会用,不要求会写) DBUtils à commons-dbuti ...

  7. QQ-AR助人教版小学英语“动”起来

    日前,人教数字出版公司与腾讯QQ达成合作,将以小学英语3-6年级8本课本为合作试点,共同推出全国首个可AR识别的课本,在QQ-AR的帮助下,课本也能“动”起来,更加生动立体地展现在孩子眼前,让学习变得 ...

  8. python用pyinstaller打包成exe文件

    版本为Python2.7 一.安装Pyinstaller 1.安装pywin32 下载安装文件:查找到跟自己适用的python版本及window系统版本匹配的pywin32,下载后安装  使用pip命 ...

  9. 005-线程sleep、join、yield、wait、notify、notifyAll、run、start、synchronized

    一.线程sleep join yield wait 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchroni ...

  10. Exception in thread "main" java.lang.NoClassDefFoundError: scala/Product$class

    在使用spark sql时一直运行报这个错误,最后仔细排查竟然是引入了两个scala library .去除其中一个scala的编译器即可 Exception in thread "main ...