当前位置:网站首页>【8、一维前缀和】
【8、一维前缀和】
2022-06-22 02:32:00 【小呆鸟_coding】
前缀和
前缀和与差分是一对逆运算
思路:
原数 :a1,a2,a3,a4 ,…, an
求前缀和 Si = a1 + a2 + a3 + a4 +…+ an
需要解决的俩个问题:
- 如何求Si
for (i = 1; i <=n ; i ++) s[i] = s[i - 1] + ai;
前缀和作用
能够快速的求出数组中一段数的和 ,求数组中[ l, r ],这一段的和时间复杂度
求数组中[ l , r ]这一段的数的和,如果暴力的方法时间复杂度为o(n),
Sr - S(l - 1) 的时间复杂度为o(1)
注意事项
- 定义数组的时候,第一个数下表从1开始。
- 需要定义S0 = 0。
之所以定义S0,是为了可以处理边界,例如求[ 1 , 10 ]这一段的数的和,此时需要用S10 - S0.实际就是S10,但是为了统一都可以使用Sr - S(l - 1) 。不用在多一个判断了。少一个if判断
题目
输入一个长度为 n 的整数序列。
接下来再输入 m 个询问,每个询问输入一对 l,r。
对于每个询问,输出原序列中从第 l个数到第 r 个数的和。
输入格式
第一行包含两个整数 n 和 m。
第二行包含 n 个整数,表示整数数列。
接下来 m 行,每行包含两个整数 l和 r,表示一个询问的区间范围。
输出格式
共 m 行,每行输出一个询问的结果。
数据范围
1≤ l ≤ r ≤ n
1 ≤ n,m ≤ 100000
−1000 ≤ 数列中元素的值 ≤ 1000输入样例:
5 3 2 1 3 6 4 1 2 1 3 2 4输出样例:
3 6 10
代码
#include <iostream> using namespace std; const int N = 100010; int n, m; int a[N], s[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i ++) scanf("%d", &a[i]); //数组初始化 for (int i = 1; i <= n; i ++) s[i] = s[i - 1] + a[i]; while (m --) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", s[r] - s[l - 1]); } return 0; }
边栏推荐
- Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
- Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
- Using hook based on xposed framework
- [proteus simulation] INT0 and INT1 interrupt count
- 国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
- Matlab learning notes (5) import and export of MATLAB data
- 2022钎焊考试模拟100题及答案
- 并查集dsu
- 微信小程序影视评论交流平台系统毕业设计毕设(4)开题报告
- Create RT_ Thread thread
猜你喜欢

Rely on the robustness of trusted AI to effectively identify deep forgery and help banks fight identity fraud

Creating and extending XFS file system based on LVM

With the acceleration of industry wide digital transformation, what kind of storage will be more popular?

Completion of graduation design of wechat small program film and television review and exchange platform system (5) assignment

Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition

微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告

Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation

Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application

MATLAB 学习笔记(5)MATLAB 数据的导入和导出

Database daily question - day 19: top ranked travelers
随机推荐
Kubernetes code generator use
微软 IE 浏览器于 6 月 15 日被永久关闭
rt_ Thread thread management
论文笔记: 多标签学习 ACkEL
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
微信小程序影视评论交流平台系统毕业设计毕设(6)开题答辩PPT
discuz! Bug in the VIP plug-in of the forum repair station help network: when the VIP member expires and the permanent member is re opened, the user group does not switch to the permanent member group
理想L9正式发布:8月底前开始交付 零售价45.98万元
PMP项目管理知识该如何运用?
fatal error: png++/png.hpp: 没有那个文件或目录
Wechat applet film and television comment exchange platform system graduation design completion (6) opening defense ppt
Anaconda historical version download
2022 brazing test simulation 100 questions and answers
Game Jam开发周期
微信小程序影视评论交流平台系统毕业设计毕设(3)后台功能
Completion of graduation design of wechat small program film and television review and exchange platform system (5) assignment
优秀的 Verilog/FPGA开源项目介绍(二十七)- 小型CPU
The "cloud" end of the 6th world intelligence conference will be held soon
Leetcode 513 find the value in the lower left corner of the tree [bfs binary tree] the leetcode path of heroding
使用 Neo4j 沙箱学习 Neo4j 图数据科学 GDS