当前位置:网站首页>【10. 差分】
【10. 差分】
2022-06-27 07:42:00 【小呆鸟_coding】
一维差分
- 前缀和与差分是逆运算
例如:
数组 a1 , a2,a3,a4,a5 ,…,an
构造 b1,b2,b3,b4,b5,…,bn
使得 ai = b1 + b2 + b3 + b4 + … + bi。c此时 ai叫做 bi的前缀和,b 叫做 ai的差分.
差分的用途:
前缀和主要是计算数组中的某个区间 [ l, r ],中间的所有数的和。
而差分主要是为了计算在[ l, r ]这个区间中所有的数全部加上一个常数c。优点:
正常遍历数组中[ l , r ]区间的话,需要的是o(n)的复杂度,但是使用差分直接是o(1)的复杂度。差分只需要俩步计算。
步骤:
- 要对数组 a的
[ l , r]区间的所有数全部加 +1,等价于b[ l ] + 1,(因为a数组是b数组的前缀和,bl +1,相当于把数组a中下标 > l 的所有数全部 +1)- 但是我们希望的是
只对[ l , r]这个区间数+1,经过上述运算,r后面的数也同时 +1,此时通过b[ r + 1]进行 -1 操作。
图解
题目
输入一个长度为 n 的整数序列。
接下来输入 m 个操作,每个操作包含三个整数 l,r,c,表示将序列中 [l,r][l,r] 之间的每个数加上 c。
请你输出进行完所有操作后的序列。
输入格式
第一行包含两个整数 n 和 m。
第二行包含 n 个整数,表示整数序列。
接下来 m 行,每行包含三个整数 l,r,c表示一个操作。
输出格式
共一行,包含 n 个整数,表示最终序列。
数据范围
1 ≤ n,m ≤ 100000
1 ≤ l ≤ r ≤ n
−1000 ≤ c ≤ 1000
−1000 ≤ 整数序列中元素的值 ≤1000输入样例:
6 3 1 2 2 1 2 1 1 3 1 3 5 1 1 6 1输出样例:
3 4 5 3 4 2
代码
#include <iostream> using namespace std; const int N = 100010; int n, m; int a[N], b[N], s[N]; void insert(int l, int r, int c) //核心公式 { b[l] += c; b[r + 1] -= c; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i ++) scanf("%d", &a[i]); while (m --) { int l, r, c; scanf("%d%d%d", &l, &r, &c); insert(l, r, c); } for (int i = 1; i <= n; i ++) b[i] += b[i - 1] ; //前缀和公式 for (int i = 1; i <= n; i ++) { a[i] += b[i]; cout << a[i] << " "; } return 0; }
边栏推荐
- c的时间函数算效率
- R language analyzing wine data
- RNA SEQ data analysis in R - investigate differentially expressed genes in the data!
- 一個人管理1000臺服務器?這款自動化運維工具一定要掌握
- 移动安全工具-jad
- 什么是浮选机?
- Delay queue `delayqueue`
- C how to call line and rows when updating the database
- Sword finger offer 07 Rebuild binary tree
- 野风药业IPO被终止:曾拟募资5.4亿 实控人俞蘠曾进行P2P投资
猜你喜欢

Programming life - what do you think of the 35 year old bottleneck of programmers?

MySQL
![[compilation principles] review outline of compilation principles of Shandong University](/img/a6/b522a728ff21085411e7452f95872a.png)
[compilation principles] review outline of compilation principles of Shandong University

Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file

Goodbye, agile Scrum

js判断用户输入的数是否为质数(多种方法)
![log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.

2022爱分析· IT运维厂商全景报告

win命令行中导入、导出数据库相关表

File and multipartfile overview
随机推荐
js中如何查看程序运行时间(计时器)
语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】
RNA SEQ data analysis in R - investigate differentially expressed genes in the data!
Guava tutorial collect some cases and write Google tool classes slowly
延时队列`DelayQueue`
Set the address book function to database maintenance, and add user name and password
Origin of forward slash and backslash
Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
JS example print the number and sum of multiples of all 7 between 1-100
js来打印1-100间的质数并求总个数优化版
Sword finger offer 07 Rebuild binary tree
Publications under nature, science and cell
js用switch语句根据1-7输出对应英文星期几
guava 定时任务
postgreSQL在windows系统遇到权限否认(permission denied)
Speech signal processing - concept (4): Fourier transform, short-time Fourier transform, wavelet transform
c的时间函数算效率
js打印99乘法表
How to view program running time (timer) in JS
JS output shape
