当前位置:网站首页>acwing795 前缀和(一维)
acwing795 前缀和(一维)
2022-06-12 16:08:00 【_刘小雨】
输入一个长度为 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

我们需要思考:
- 如何求Si
- 作用: [l, r] 的和 = S- S(l-1) 一次计算就能得到结果 O(n)
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int a[N], s[N];
int main()
{
// 数据范围有点大, 用scanf 更好
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
// 这里从1开始,有便于计算前缀和
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;
}
边栏推荐
- Keep an IT training diary 067- good people are grateful, bad people are right
- Unicom Network Management Protocol block diagram
- 超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群
- (4) Googlenet replay
- 面试:了解装箱和拆箱操作吗?
- 任务 水果炸汁机 0611
- Global and Chinese market of medical ECG telemetry equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- < 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
- MySQL blob and text types
- PHP builds a high-performance API architecture based on sw-x framework (II)
猜你喜欢

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)

C packing and unpacking

RTOS rt-thread裸机系统与多线程系统

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)

Analysis on the development status and direction of China's cultural tourism real estate industry in 2021: the average transaction price has increased, and cultural tourism projects continue to innova

IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码
![In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]](/img/dd/1bf44d284c709b6bebd4b308ba2cee.jpg)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]

从斐波那契数列求和想到的俗手、本手和妙手

Project training of Software College of Shandong University rendering engine system point cloud processing (10)

如何使用Grafana轻松实现OVL数据可视化
随机推荐
FPGA (III) trigger and latch
【架构优化过程思考】如何构建技术方案影响的评估能力
Install RHEL 7/8 (red hat) virtual machine (Reprint)
Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
Difference between tinyint and int
Scanpy(六)空间转录组数据的分析与可视化
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
面试:‘==‘与equals()之间的区别
Task fruit Juicer 0611
Unity get local video / download network video
Defer learning in golang
【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
Application of postman-rest client plug-in
当编程纳入到高考。。。
Keep an IT training diary 067- good people are grateful, bad people are right
Divide training set, test set and verification set
Project training of Software College of Shandong University rendering engine system basic renderer (6)
mysql Blob和Text类型
Escape rules and examples of go