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

用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. C语言数组求长度

    1.创建数组 ,,,,};/*创建一个int型数组,数组的长度为5*/ 2.利用sizeof求一维数组长度 int len; len = sizeof(a)/sizeof(int); Δ以上求数组长度 ...

  2. odoo学习总结

                                                   odoo10总结 1.odoo中的向导应用. .py文件 # -*- coding: utf-8 -*-f ...

  3. 支持异步同步的分布式CommandBus MSMQ实现 - 支持Session传递、多实例处理

    先上一张本文所描述的适用场景图 分布式场景,共3台server: 前端Server Order App Server Warehouse App Server 功能: 前端Server可以不停的发送C ...

  4. .net core 2.1 开源项目 COMCMS dnc版本

    项目一直从dotnet core 1.1开始,升级到2.0,乃至如今2.1,以后保持继续更新. 但可能只是一个后台,前台的话,到时候看有没有好的模板. ------------无聊的分割线------ ...

  5. 朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上)

    朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上) [下载本文PDF进行阅读] 设计模式是前人通过大量的实践总结出来的一些经验总结和最佳实践.在经过多年的软件开发实践之后,回过头来去看23种设 ...

  6. Django+nginx+uwsgi部署教程

    00-所需工具 xshell:https://www.netsarang.com/zh/downloading/?token=ZlZnVUNsWDJuM0VaZnVPUjZST1dwd0AzYlNte ...

  7. Java 自动装箱与拆箱(Autoboxing and unboxing)

    什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象实例的时候,我们会这样: Class a = ...

  8. A4纸尺寸 web打印报告

    A4纸对应的像素尺寸: <style> @media print { .Noprn{ display:none;} .print-hidden { display: none !impor ...

  9. 动态规划-数位dp

    大佬讲的清楚 [https://blog.csdn.net/wust_zzwh/article/details/52100392] 例子 不要62或4 l到r有多少个数不含62或者4 代码 #incl ...

  10. 原生jS之-去掉字符串开头和结尾的空字符

    怎么解决这个问题?? 思路就是我们利用正则匹配到所谓的空格,然后替换为空字符,我们要用到的是str的replace API 代码如下: <!DOCTYPE html> <html l ...