POJ 3468 A Simple Problem with Integers(线段树:区间更新)
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(线段树:区间更新)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- 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 ...
- 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 ...
随机推荐
- Egret Wing4.0.3 合并资源图片问题
一 发布项目时,选择合并图片资源 选择合图大小 发布后,图片合并.随机了图片名字. 二 随机名的问题 当资源不变更的情况下,多次发布,每次发布后资源的图片随机名是不变的. 现在改变preload组 ...
- nodejs 重定向 (redirect + writeHead(Location))
参考: Node.js实现301.302重定向服务 Express URL跳转(重定向)的实现:res.location()与res.redirect() 一 方式1 index.js var htt ...
- Android搜索自动提示功能 AutocompleteTextView
1.配置main.xml中自动提示控件: <AutoCompleteTextView android:id="@+id/autotv_searchresult" androi ...
- OC开发_Storyboard——UITableView
一.tableView 1.datasource数据源 (1 构造每一个tableVIewCell的方法:cellForRowAtIndexPath,这里的 dequeueReusableCellWi ...
- ionic+cordova开发!
这里是一些学习的过程中纪录的: 官方网站: http://www.ionic-china.com/ 参考文章: https://blog.csdn.net/xyzz609/article/detail ...
- C++ list容器系列功能函数详解
C++ list函数详解 首先说下eclipse工具下怎样debug:方法:你先要设置好断点,然后以Debug方式启动你的应用程序,不要用run的方式,当程序运行到你的断点位置时就会停住,也会提示你进 ...
- 【node】------mongoose的基本使用------【巷子】
1.安装mongoose npm install mongoose 2.启动数据库 mongod --dbpath d:\data\db 3.引入mongoose模块并连接数据库 const mong ...
- ubuntu ---QQ install/desktop/ibus reinstall
http://www.linuxidc.com/Linux/2016-09/134923.htm ( Ubuntu 16.04安装QQ国际版图文详细教程) [ sudo apt-get install ...
- Currency Exchange---poj1860 ( spfa, 回路,最长路)
题目链接:http://poj.org/problem?id=1860 题解: 两种情况YES,一种是存在正权回路: 一种是求最长路后,实现了增值,也是YES: 用spfa来判断是否存在正权回路,其实 ...
- CMDB经验分享之 – 剖析CMDB的设计过程
作为IT管理的核心,CMDB逐渐成为系统管理项目实施的热点.在很多的案例中,由于忽视了CMDB的因素,ITIL的深入应用受到了极大的挑战.同时,由于CMDB是IT管理信息的集中,CMDB也是一个重要的 ...