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

用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# zip -ICSharpCode.SharpZipLib

    利用第三方组件 ICSharpCode.SharpZipLib   download from:  https://github.com/icsharpcode/SharpZipLib using S ...

  2. Java和.NET(C#)的开发用到的技术对比总结

    前言 声明:我指的是一般的Java和.NET(C#)的后台开发用到的技术总结 最近一直在应聘ing,楼主的项目还是.NET(C#)项目居多,Java项目相对少,在这也吐槽下,招.NET(C#)的公司实 ...

  3. win 2008 R2 或以上版本,只有C盘情况下,PHP上传文件,显示不了解决办法

    主要问题:因为没权限 解决办法:给C:\Windows\Temp 加上IIS账户读写权限

  4. 【React】开发一个城市选择控件

    想到做这个,是因为无意中在github上看到了这一个仓库https://github.com/lunlunshiwo/ChooseCity,做的就是一个城市选择控件,是用vue写的,说的是阿里的一道题 ...

  5. [LeetCode] 56 - Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. hibernate中实体与数据库中属性对应的类型

    常用的字段及类型,在数据库中字段名称若与实体对应的属性字段名称相同,hibernate可以自动映射,在一些情况下hibernate可能报错这时候有的错误可以通过指定对应的类型避免.下面给出一些常用的 ...

  7. Python入门-用户登录程序

    _flag = Falsecount = 0users = [['ziv', '123'], ['alex', '12345']]while count < 3: username = inpu ...

  8. (第十三周)评论Final发布I

    本人所在组:奋斗吧兄弟 按课上展示的顺序对每组进行点评: 1.  Nice 项目:约跑软件 展示的时候使用了摄像头投影,提高了演示效果,软件的背景进行了美化,表现好了很好.解决了我们组提出的文字多挤没 ...

  9. Choosing The Commander CodeForces - 817E (01字典树+思维)

    As you might remember from the previous round, Vova is currently playing a strategic game known as R ...

  10. git reset的用法

    git reset三个选项 --mix,--hard,--soft 数据 针对每个选项都是操作这个文件. [root@centos demo]# git init Initialized empty ...