A Simple Problem with Integers(线段树区间更新模板)
最基本的线段树的区间更新及查询和
用tag(lazy)数组来“延缓”更新,查询或添加操作必须进行pushdown操作,即把tag从p传到lp和rp并清楚tag[p],既然得往lp和rp递归,那么就可以“顺便”往下传
pushdown操作代码
inline void pushdown(int p, int llen, int rlen) {
if (tag[p]) {
tag[lp] += tag[p], tag[rp] += tag[p];
tree[lp] += tag[p] * llen;
tree[rp] += tag[p] * rlen;
tag[p] = ;
}
}
还要注意必须用long long来存值
代码如下
#include <cstdio>
#include <algorithm>
#define ll long long
#define lp p<<1
#define rp p<<1|1
using namespace std; const int maxn = ;
ll tree[maxn<<], tag[maxn<<];
void build(int p, int l, int r) {
if (l == r) {
scanf("%lld", &tree[p]);
return;
}
int mid = (l + r) >> ;
build(lp, l, mid); build(rp, mid + , r);
tree[p] = tree[lp] + tree[rp];
}
inline void pushdown(int p, int llen, int rlen) {
if (tag[p]) {
tag[lp] += tag[p], tag[rp] += tag[p];
tree[lp] += tag[p] * llen;
tree[rp] += tag[p] * rlen;
tag[p] = ;
}
}
void add(int p, int l, int r, int x, int y, int z) {
if (x <= l && y >= r) {
tag[p] += 1LL * z;
tree[p] += 1LL * z * (r - l + );
return;
}
int mid = (l + r) >> ;
pushdown(p, mid - l + , r - mid);
if (x <= mid) add(lp, l, mid, x, y, z);
if (y > mid) add(rp, mid + , r, x, y, z);
tree[p] = tree[lp] + tree[rp];
} ll find(int p, int l, int r, int x, int y) {
if (x <= l && y >= r) return tree[p];
int mid = (l + r) >> ;
pushdown(p, mid - l + , r - mid);
if (y <= mid) return find(lp, l, mid, x, y);
if (x > mid) return find(rp, mid + , r, x, y);
return find(lp, l, mid, x, y) + find(rp, mid + , r, x, y);
} int main() {
int n, q;
scanf("%d%d", &n, &q);
build(, , n);
while (q--) {
char s[];
int x, y;
scanf("%s%d%d", s, &x, &y);
if (s[] == 'Q') {
printf("%lld\n", find(, , n, x, y));
} else {
int z; scanf("%d", &z);
add(, , n, x, y, z);
}
}
return ;
}
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 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- [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 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 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标记,延迟标记, ...
- A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- 线程的synchronized、volatile及原子操作
public class ThreadDemo7{ //structs2线程不安全 共享变量 //n++ 复合操作 对于volatile修饰的变量不安全 //原子操作 int value; //让方法 ...
- Sql Server利用游标批量清空数据表
先吐槽一下,由于公司要为新客户部署一个全新的系统,然而公司并没有空库,所以只能把正在线上运行的数据库给备份,然后清空相关数据 下面分享一下我在做清空数据库时写的一个批量清空数据表的方法 思路:查询出该 ...
- Linux、Windows如何进行性能监控与调优
1.Linux命令行工具 推荐:CentOS 7 1.1 top命令 top命令的输出如下: top命令的输出可以分为两部分:前半部分是系统统计信息,后半部分是进程信息.在统计信息中, 第1行是任务队 ...
- 深入理解Spring Boot数据源与连接池原理
Create by yster@foxmail.com 2018-8-2 一:开始 在使用Spring Boot数据源之前,我们一般会导入相关依赖.其中数据源核心依赖就是spring‐boot‐s ...
- Python-os模块-60
os 模块: 和操作系统打交道的模块 os模块是与操作系统交互的一个接口 os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirna ...
- H5 31-CSS元素显示模式转换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vector源码分析
Vector与ArrayList底层实现基本类似,底层都是用数组实现的,最大的不同是Vector是线程安全的.ArrayList源码分析请参考ArrayList源码分析 一.源码分析 基于jdk1.7 ...
- mybatis之批量插入
一.导入功能优化 普通for循环,对于导入大量数据时非常耗时.可以通过Mybatis的批量插入功能提高效率.每批次导入的数据不能太多,否则会报错.通过测试发现,每批次200条为宜. 测试结果: 开启事 ...
- 阿里云ECS服务器云监控(cloudmonitor)Go语言版本插件安装卸载与维护
云监控Go语言版本插件安装_主机监控_用户指南_云监控-阿里云https://help.aliyun.com/document_detail/97929.html 云监控cloudmonitor 1. ...
- Linux之基础知识
在此总结使用Linux的时候,一些必须知道的基础内容,记录一下,加强记忆 一. linux 运行级别 运行级别就是操作系统当前正在运行的功能级别.这个级别从0到6 ,具有不同的功能.这些级别在/et ...