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 ...
随机推荐
- JS获取url中的指定参数
function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...
- HTML(下)
目录 HTML(下) form表单 表单功能 表单属性 <input>输入标签(文本框)(内联标签) <select>下拉列表标签(内联标签) <textarea> ...
- 安装gitlab ce
切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...
- Wizard's Tour CodeForces - 860D (图,构造)
大意: 给定$n$节点$m$条边无向图, 不保证连通, 求选出最多邻接边, 每条边最多选一次. 上界为$\lfloor\frac{m}{2}\rfloor$, $dfs$贪心划分显然可以达到上界. # ...
- 关于redis的几件小事(十)redis cluster模式
redis cluster是redis提供的集群模式. 1.redis cluster的架构 ①可以有多个master node,每个master node 都可以挂载多个slave node. ②读 ...
- Vue与Angular以及React的三者之间的区别
1.与AngularJS的区别 相同点:都支持指令:内置指令和自定义指令:都支持过滤器:内置过滤器和自定义过滤器:都支持双向数据绑定:都不支持低端浏览器. 不同点:AngularJS的学习成本高,比如 ...
- Linux Exploit系列之四 使用return-to-libc绕过NX bit
使用return-to-libc绕过NX bit 原文地址:https://bbs.pediy.com/thread-216956.htm 这篇讲解的比较好,主要的问题是获得system地址和exit ...
- zabbix磁盘的自动发现与磁盘指标监控
由于最近项目上需要对服务器监控进行规范化监控,再磁盘这块有几种方式 1.如果每台设备的磁盘是一样的 比如都有vda,vdb两块磁盘那么可以采用 1.1 每台客户端写脚本,服务端每台设备去加上监控项(- ...
- On Java 8
On Java 8本书原作者为 [美] Bruce Eckel,即<Java 编程思想>的作者.本书是事实上的 <Java 编程思想>第五版.<Java 编程思想> ...
- linux版宝塔安装Redis
1安装服务 2配置设置 3安装PHP扩展 首先,我们来安装服务,进入管理面板--软件管理--运行环境--redis-点击安装,等待完成 完成之后开始第二步,配置设置.这一步根据自己需要进行配置.注意安 ...