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 ...
随机推荐
- python的内存管理与垃圾回收机制学习
一.python内存申请: 1.python的内存管理分为六层:最底的两层有OS控制.第三层是调用C的malloc和free等进行内存控制.第四层第五层是python的内存池.最上层使我们接触的直接对 ...
- Android - 采用 SharedPreferences 存储数据
SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data/data/<包名& ...
- 编程中,static的用法详解
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用.一.面向过程设计中的sta ...
- vue 缓存的keepalive页面刷新数据
用到这个的业务场景是这样的: a页面点击新建列表按钮进入到新建的页面b,填写b页面并点击b页面确认添加按钮,把这些数据带到a页面,填充到列表(数组),可以添加多条, 点击这条的时候进入到编辑页面,确认 ...
- postgresql架构基础(转)-(1)
PostgreSQL使用一种客户端/服务器的模型.一次PostgreSQL会话由下列相关的进程(程序)组成: 一个服务器进程,它管理数据库文件.接受来自客户端应用与数据库的联接并且代表客户端在数据库上 ...
- Xshell配置ssh免密码登录-密钥公钥(Public key)
1 简介 ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口令(密码)认证方式是我们最常用的一种,这里介绍密钥认证方式登录到linux/unix的方法. 使用密钥登录分为3步: 1 ...
- Uva10917 Walk Through the Forest
题目链接:https://vjudge.net/problem/UVA-10917 题目意思:Jimmy下班回家要闯过一下森林,劳累一天后在森林中散步是非常惬意的事,所以他打算每天沿着一条不同的路径回 ...
- Oracle管理监控之监控表空间使用率脚本
SELECT D.TABLESPACE_NAME, SPACE "SUM_SPACE(M)", BLOCKS SUM_BLOCKS, SPACE ...
- rac下asm管理的表空间-数据文件的重命名
asm下表空间的重命名与普通文件系统下的表空间重命名原理是一样的,只不过asm管理的数据文件有一些需要注意的地方,另外在asm下操作数据文件需要格外小心,稍有不慎将会造成数据文件丢失,如可以做备份最好 ...
- Django - Jsonp、CORS
一.同源策略 https://www.cnblogs.com/yuanchenqi/articles/7638956.html 同源策略(Same origin policy)是一种约定,它是浏览器最 ...