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 ...
随机推荐
- 【BZOJ2118】墨墨的等式 最短路
[BZOJ2118]墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值 ...
- {sharepoint}提升 SharePoint 代码执行权限
提升 SharePoint 代码执行权限 关于如何提升 SharePoint 代码执行权限及相关知识介绍的文章我们园子里有很多, 这里给出其中两篇文章的链接,就不再啰嗦了. http://www.cn ...
- windows 下,go语言 交叉编译
http://bbs.csdn.net/topics/390601048 参考上面的操作
- Android动态添加布局
//1.利用LayoutInflater的inflate动态加载XML mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID) ...
- LINQ中的连接(join)用法示例
Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的 ...
- pta 习题集 5-2 找出不是两个数组共有的元素 (5分)
给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数NN(≤20≤20),随后是NN个整数,其间以空格分隔. 输出格式: 在一行中按照数字 ...
- os模块os.walk() 方法和os.path.join()的简单使用
os.walk: http://www.runoob.com/python/os-walk.html os.path.join: https://blog.csdn.net/zmdzbzbhs ...
- CentOS7下的YUM源服务器搭建详解,过程写的很详细(转)
因为近期公司需要搭建一个YUM源服务器给大量的linux(mini)使用,所以因此在网上找了很多的教程,却没有一个特别详细的,很多都有遗漏,参差不齐.所以,打算自己做完之后方便以后查阅,特出此文档. ...
- Django RF:学习笔记(8)——快速开始
Django RF:学习笔记(8)——快速开始 安装配置 1.使用Pip安装Django REST Framework: pip install djangorestframework 2.在Sett ...
- 1、安装electron
安装electron安装并非一帆风顺,我有FQ哈,所以网络方面我就不说了,你们不行的话,可以用cnpm,我说的是另一个问题 我是这样解决的,用以下命令就好了 sudo npm install -g e ...