最基本的线段树的区间更新及查询和

用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(线段树区间更新模板)的更多相关文章

  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. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  4. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  6. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  7. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  9. 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 ...

  10. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. redis学习(四)——Hash数据类型

    一.概述 我们可以将Redis中的Hash类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Age等. ...

  2. Java NIO:IO与NIO的区别

    一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...

  3. 在Sublime中配置JsFormat

    JsFormat配置文件: { // exposed jsbeautifier options "indent_with_tabs": false, // 保留换行符 " ...

  4. EntityFramework Core并发导致显式插入主键问题

    前言 之前讨论过EntityFramework Core中并发问题,按照官网所给并发冲突解决方案以为没有什么问题,但是在做单元测试时发现too young,too simple,下面我们一起来看看. ...

  5. babel-polyfill使用与性能优化

    文章首发于笔者的个人博客 文章概览 本文主要内容包括:什么是babel-polyfill,如何使用,如何通过按需加载进行性能优化. 本文所有例子可以在 笔者的github 找到. 什么是babel-p ...

  6. 快速导入导出Oracle数据demo(sqlldr、UTL_FILE)

    本文演示快速sqlldr导入.UTL_FILE导出Oracle表数据实例 表结构如下,演示数据约112万,可自行准备. create table MemberPointDemo ( MEMBERID ...

  7. CentOS 7从Python 2.7升级至Python3.6.1

    引言: CentOS是目前最为流行的Linux服务器系统,其默认的Python 2.x,但是根据python社区的规划,在不久之后,整个社区将向Python3迁移,且将不在支持Python2, 那该如 ...

  8. 逻辑回归为什么用sigmoid函数

    Logistic回归目的是从特征学习出一个0/1分类模型,而这个模型是将特性的线性组合作为自变量,由于自变量的取值范围是负无穷到正无穷. 因此,使用logistic函数(或称作sigmoid函数)将自 ...

  9. 福州大学软件工程1816 | W班 第4次作业(团队展示)成绩排名

    作业链接 评分细则 队员姓名与学号(标记组长),其中4-7人一组,特殊情况经老师允许后可以突破限制:(1分) 队名(体现项目内容,并要求有亮点与个性):(1分) 拟作的团队项目描述:一句话(中英文不限 ...

  10. 福州大学软件工程1816 | W班 第10次作业[个人作业——软件产品案例分析]

    作业链接 个人作业--软件产品案例分析 评分细则 本次个人项目分数由两部分组成(课堂得分(老师/助教占比60%,学生占比40%)满分40分+博客分满分60分) 课堂得分和博客得分表 评分统计图 千帆竞 ...