HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
#include <iostream>
#include <cstdio> using namespace std; const int maxn = 1e5+; typedef long long ll; struct node {
int l, r;
ll sum;
}tree[maxn << ]; int n, m;
ll arr[maxn];
ll lazy[maxn << ]; //懒惰数组 void pushup(int root) {
tree[root].sum = tree[root << ].sum + tree[root << | ].sum;
} void build(int root, int l ,int r) {
tree[root].l = l;
tree[root].r = r;
lazy[root] = ;
if(l == r) {
tree[root].sum = arr[l];
return;
}
int mid = (l + r) >> ;
build(root << , l, mid);
build(root << | , mid + , r);
pushup(root);
} void spread(int root) {
if(lazy[root]) { //判断延迟标记是否存在
lazy[root << ] += lazy[root]; //左子树下传延迟标记
lazy[root << | ] += lazy[root]; //右子树下传延迟标记
tree[root << ].sum += lazy[root] * (tree[root << ].r - tree[root << ].l + ); //更新左结点信息
tree[root << | ].sum += lazy[root] * (tree[root << | ].r - tree[root << | ].l + ); //更新右结点信息
lazy[root] = ; //清除延迟标记
}
} void update(int root, int x, int y, ll val) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) {
tree[root].sum += (r - l + ) * val;
lazy[root] += val; //添加延迟标记
return;
}
spread(root); //下传延迟标记
int mid = (l + r) >> ;
if(x <= mid) {
update(root << , x, y, val);
}
if(y > mid) {
update(root << | , x, y, val);
}
pushup(root);
} ll query(int root, int x, int y) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) { //完全覆盖
return tree[root].sum;
}
spread(root); //下传延迟标记
ll res = ;
int mid = (l + r) >> ;
if(x <= mid) {
res += query(root << , x, y);
}
if(y > mid) {
res += query(root << | , x, y);
}
return res;
} int main() {
while(~scanf("%d %d", &n, &m)) {
for(int i = ; i <= n; i++) {
scanf("%lld", &arr[i]);
}
build(, , n);
while(m--) {
char op[];
int l, r;
scanf("%s", op);
if(op[] == 'Q') {
scanf("%d %d", &l, &r);
printf("%lld\n", query(, l, r));
} else {
ll val;
scanf("%d %d %lld", &l, &r, &val);
update(, l, r, val);
}
}
}
return ;
}
HDU 3468:A Simple Problem with Integers(线段树+延迟标记)的更多相关文章
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [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 ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- 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 (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- FFmpeg4.0笔记:采集系统声音
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集系统声音 void test_systemsound() ...
- 实现一台Linux电脑连接另一台Linux(SSH实现linux之间的免密码登陆)
怎么实现一台Linux电脑连接另一台Linux电脑? 首先查看是否安装ssh服务:systemctl status sshd.service 启动服务:systemctl start sshd.ser ...
- solr学习笔记-增加mmesg4J中文分词
solr版本6.1.centos6.7.mmesg4j版本2.30 solr安装目录:/usr/local/solr-6.1.0 1.下载mmesg4j包: 地址:https://github.com ...
- golang(6): 接口 & 反射
接口详解 // 举例:sort包中的 Sort 函数,如下: func Sort(data Interface) Sort sorts data. It makes one call to data. ...
- js中new到底做了什么?如何重写new?
new 构造函数()执行顺序1.在堆中开辟对象内存空间, 记为obj2.在obj 中添加__proto__属性并指向 构造函数.prototype3.将构造函数中的this 指向obj4.执行构造函数 ...
- Reference to ‘xxxxx’ is ambiguous 错误
1.原因,在当前类重复引入了 类库,比如 pch里面导入了#import "XXX" 此类的.h 又引入 #import <xxx/xxx> 导致 解决方法:删除此类的 ...
- this全面解析<转>
先搞明白一个很重要的概念 —— this的值是在执行的时候才能确认,定义的时候不能确认! 为什么呢 —— 因为this是执行上下文环境的一部分,而执行上下文需要在代码执行之前确定,而不是定义的时候. ...
- vue 内存数组变化监听
watch: { carts: { handler(val, oldVal) { subtotal(this.carts); console.log(this.carts) }, deep: true ...
- ssh无密码访问设置(ssh-keygen 的详解)
[原文链接]http://blog.csdn.net/wh_19910525/article/details/7433164 为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签 ...
- 高大上的微信小程序中渲染html内容—技术分享
大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 解决方案 wxP ...