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 ...
随机推荐
- codeforces 620C
题目链接:https://codeforces.com/problemset/problem/620/C 题目分析 题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一 ...
- XPath读取xml文件
1.创建解析工厂 2.创建解析器 3.读xml文件,生成w3c.docment对象树 4.创建XPath对象 5.通过路径查找对象 例子: import javax.xml.parsers.Docum ...
- FFmpeg4.0笔记:封装ffmpeg的音频重采样功能类CSwr
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSwr.h /************************* ...
- Java小程序—录屏小程序(上半场)
做软件的三个步骤: (1)做什么? (2)怎么做? (3)动手做! ok,我们今天要做的是一个录屏软件,那怎么做呢?首先,我们小时候都玩过一种小人书,就是当你快速翻动书页时,书中的人物就会活灵活现的动 ...
- leecode刷题(26)-- 用栈实现队列
leecode刷题(26)-- 用栈实现队列 用栈实现队列 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返 ...
- mongoDB数据库文件路径和数据操作
1.查看MongoDB在电脑上的安装路径 which mongod 2.默认mongodb 数据文件是放到根目录 data/db 文件夹下,如果没有这个文件,需要自行创建 mkdir -p /data ...
- luogu题解 P1707 【刷题比赛】矩阵加速递推
题目链接: https://www.luogu.org/problemnew/show/P1707 分析: 洛谷的一道原创题,对于练习矩阵加速递推非常不错. 首先我们看一下递推式: \(a[k+2]= ...
- vue--微信支付
1.当前页面没有注册: 需要登录微信支付商家后台,添加支付路径授权 2.安装 weixin-js-sdk 微信SDK npm install weixin-js-sdk --save 3.引入 imp ...
- 代码调试console对象的花式玩法
转自阮一峰http://www.ruanyifeng.com/home.html console.log(),console.info(),console.debug() console.log方法用 ...
- LeetCode——回文链表
题目 给定一个链表的头节点head,请判断该链表是否为回 文结构. 例如: 1->2->1,返回true. 1->2->2->1,返回true. 15->6-> ...