Description

You have \(N\) integers, \(A_1, A_2, ... , A_N\). 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 \(A_1, A_2, ... , A_N\). \(-1000000000 \le A_i \le 1000000000\).

Each of the next \(Q\) lines represents an operation.

"\(C\ a\ b\ c\)" means adding c to each of \(A_a, A_{a+1}, \ ...\ , A_b\). \(-10000 \le c \le 10000\).

"\(Q\ a\ b\)" means querying the sum of \(A_a, A_{a+1}, \ ...\ , A_b\).

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.

Solution

题意

给定 \(n\) 个数和 \(q\) 个询问,询问包含两种:\(C\ a\ b\ c\) 代表区间 \([a, b]\) 的每个数加上 \(c\),\(Q\ a\ b\) 输出区间 \([a, b]\) 的和。

题解

分块

区间更新模板题,本题可以使用树状数组、线段树和分块解决,这里使用的是分块。

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std; typedef long long ll; const int maxn = 1e5 + 10; ll a[maxn], sum[maxn], add[maxn]; // add[] 是增量标记
int L[maxn], R[maxn]; // 存放每个块的左右边界
int block[maxn]; // 存放下标为 i 的元素的块号
int n, q;
int block_size; // 块的大小 // 分块 + 预处理
void init() {
block_size = sqrt(n);
for(int i = 1; i <= block_size; ++i) {
L[i] = (i - 1) * block_size + 1;
R[i] = i * block_size;
}
// 处理最后一块
if(R[block_size] < n) {
++block_size;
L[block_size] = R[block_size - 1] + 1;
R[block_size] = n;
}
// 预处理每个块的区间和
for(int i = 1; i <= block_size; ++i) {
for(int j = L[i]; j <= R[i]; ++j) {
block[j] = i;
sum[i] += a[j];
}
}
} // 将区间 [l, r] 内的所有元素加 c
void change(int l, int r, ll c) {
int p = block[l], q = block[r]; // 取出左右区间所在的块号
if(p == q) {
// 在同一块直接块内暴力
for(int i = l; i <= r; ++i) {
a[i] += c;
}
sum[p] += c * (r - l + 1);
} else {
// 不在同一块,块内暴力,块间整块处理
for(int i = p + 1; i <= q - 1; ++i) {
add[i] += c;
}
// 块内暴力
for(int i = l; i <= R[p]; ++i) {
a[i] += c;
}
sum[p] += c * (R[p] - l + 1);
for(int i = L[q]; i <= r; ++i) {
a[i] += c;
}
sum[q] += c * (r - L[q] + 1);
}
} ll query(int l, int r) {
int p = block[l], q = block[r]; // 取出左右区间所在的块号
ll ans = 0;
if(p == q) {
for(int i = l; i <= r; ++i) {
ans += a[i];
}
ans += add[p] * (r - l + 1);
} else {
// 块间暴力
for(int i = p + 1; i <= q - 1; ++i) {
ans += sum[i] + add[i] * (R[i] - L[i] + 1); // 注意不是乘以 block_size
}
// 块内暴力
for(int i = l; i <= R[p]; ++i) {
ans += a[i];
}
ans += add[p] * (R[p] - l + 1);
for(int i = L[q]; i <= r; ++i) {
ans += a[i];
}
ans += add[q] * (r - L[q] + 1);
}
return ans;
} int main() {
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
init();
for(int i = 0; i < q; ++i) {
char op;
getchar(); scanf("%c", &op);
int l, r;
scanf("%d%d", &l, &r);
if(op == 'C') {
ll c;
scanf("%lld", &c);
change(l, r, c);
} else {
printf("%lld\n", query(l, r));
}
}
return 0;
}

Reference

《算法竞赛进阶指南》 李煜东 著

POJ 3468 A Simple Problem with Integers (分块)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  2. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  3. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  4. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

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

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

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

随机推荐

  1. svm 之 线性可分支持向量机

    定义:给定线性可分训练数据集,通过间隔最大化或等价的求解凸二次规划问题学习获得分离超平面和分类决策函数,称为线性可分支持向量机. 目录: • 函数间隔 • 几何间隔 • 间隔最大化 • 对偶算法 1. ...

  2. C++——右值引用

    1.绑定到一个将要销毁的右值的引用——移动 2.左值表达式——对象的身份:右值表达式——对象的值 ; int& r=i; int&& rr=i;//×,左值不能绑定到右值引用 ...

  3. upc组队赛12 Cardboard Container【枚举】

    Cardboard Container Problem Description fidget spinners are so 2017; this years' rage are fidget cub ...

  4. vue-cesium中经纬度写反了,报错

    vue-cesium中经纬度写反了,报错 [Vue warn]: Invalid prop: custom validator check failed for prop "position ...

  5. compareTo冒泡比较时间字符串

    public static void main(String[] args) { List<String> dates = new ArrayList<String>(); d ...

  6. ASE团队项目alpha阶段Frontend组 scrum2 记录

    ASE团队项目alpha阶段Frontend组 scrum2 记录 本次会议于11.5日, 11:30在微软北京西二楼13158研讨室,讨论持续15分钟 与会人员:Jingyi Xie, Jiaqi ...

  7. JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。

    Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...

  8. react 数据发生变化,页面改变的原理

    数据发生变化,页面改变的原理: 比较虚拟的dom 不怎么损耗性能,真实的dom比较会损耗性能 1.state 数据 2.jsx 模板 3.生成虚拟的dom 3.数据和模板结合,生成虚拟的dom 4.用 ...

  9. Tomcat免安装版踩坑

    下载解压 从官网下载Tomcat的压缩包解压到硬盘上(这里用的是toncat7),解压之后目录如下(Windows) bin 存放tomcat的一些命令脚本 conf 存放配置文件 lib 存放运行时 ...

  10. getElementsBy 系列方法相比querySelector系列的区别

    最近在做的项目中,使用querySelectorAll获取了同class名的元素后,绑定onmouseover事件和onmouseout后,多次在几个元素上移入移出操作时,控制台出现了报错的问题,最后 ...