A Simple Problem with Integers
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.
 
 
算法:线段树+延迟标记
 
 
#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(线段树+延迟标记)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  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 w ...

  5. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  6. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  7. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  8. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  9. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  10. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

随机推荐

  1. Known Notation括号匹配类问题(2014年ACM/ICPC 亚洲区域赛牡丹江)

    题意: 给你数字或 * 的串,你可以交换一个*和数字.在最前面添1.在一个地方插入*,问你使串满足入栈出栈的(RNP)运算法则. 思路: 引用:https://blog.csdn.net/u01158 ...

  2. tf.strided_slice函数

    在keras_yolo中model函数下的yolo_head下:grid_shape = K.shape(feats)[1:3] grid_shape: <tf.Tensor 'strided_ ...

  3. opencv中的高维矩阵Mat

    本示例程序主要是通过实例演示高维Mat的寻址方式. //3,4分别表示行数.列数,所以3*4是一个页面的元素数,2表示有2个3*4 ,b=,c=; int size[]={a,b,c}; float* ...

  4. angular项目中ts的配置编译tsconfig.json

    { "compilerOptions": { /* 基本选项 */ "target": "es5", // 指定 ECMAScript 目标 ...

  5. 网络库Alamofire使用方法

    Github地址 由于Alamofire是swift网络库,所以,以下的所有介绍均基于swift项目 导入Alamofire 以下为使用cocoapods导入,其余的方式请参考官网 source 'h ...

  6. flaskbb部署笔记

    https://flaskbb.org/ https://github.com/sh4nks/flaskbb/ https://flaskbb.readthedocs.io/en/latest/ins ...

  7. 架构师成长之路5.1-Saltstack安装及入门

    点击架构师成长之路 架构师成长之路5.1-Saltstack安装及入门 (安装.配置.启动) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需 ...

  8. Mac OSX编译安装php5.6

    安装好OSX 10.13以后默认自带的php7.1.7,跟现有环境不兼容,所以准备编译安装php5.6,自带的php7不建议卸载,重新安装一份php5.6 1.安装php的一些依赖,推荐使用brew安 ...

  9. Lomsat gelral CodeForces - 600E (树上启发式合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  10. zencart简单设置分类链接不同css样式

    includes/templates/模板/sideboxes/tpl_categories.php $content .= '<a class="'.$new_style.'&quo ...