当前位置:网站首页>795. 前缀和
795. 前缀和
2022-08-05 11:46:00 【aJupyter】
Question
输入一个长度为 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
Ideas
一维前缀和
Code
# 一维前缀和
''' 规定s[0] = 0 [l,r]区间和s[r]-s[l-1] '''
N = 100010
s = [0 for i in range(N)]
n,m = list(map(int,input().strip().split()))
a = [0] + list(map(int,input().strip().split()))
for i in range(1,n+1):
s[i] = s[i-1] + a[i]
for i in range(m):
l,r = list(map(int,input().strip().split()))
print(s[r]-s[l-1])
边栏推荐
- Cesium.js 地形挖洞
- [7.29-8.5] Review of wonderful technical blog posts in the writing community
- 巴比特 | 元宇宙每日必读:中国1775万件数字藏品分析报告显示,85%的已发行数藏开通了转赠功能...
- Qt::qcustomplot 和 qchart数据填充相关
- 灰度值与热成像理解
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- 后缀自动机(SAM)——黑盒使用方案
- 5G NR system messages
- Web3 中的安全问题和防范
- 【着色器实现Flicker“DJ”闪烁效果_Shader效果第十五篇】
猜你喜欢

支持向量机SVM

.NET in-depth analysis of the LINQ framework (6: LINQ execution expressions)

五大理由告诉你为什么开发人员选择代码质量静态分析工具Klocwork来实现软件安全

【HMS core】【FAQ】Health Kit, Ads kit, Push Kit Typical Questions Collection 5

.NET深入解析LINQ框架(六:LINQ执行表达式)

163_技巧_Power BI 一键批量建立自定义字段参数

互联网行业凛冬之至,BATM的程序员是如何应对中年危机的?

不是吧?还有人不会定位线上MySQL慢查询问题?

“蘑菇书”是怎样磨出来的?

机器学习——集成学习
随机推荐
163_Tricks_Power BI one-click batch creation of custom field parameters
Android development with Kotlin programming language II Conditional control
四、kubeadm单master
I'm going crazy.Again A few days can not be A problem
Introduction to the Evolution of Data Governance System
【硬件架构的艺术】学习笔记(2)同步和复位
Cesium.js点线面绘制
163_技巧_Power BI 一键批量建立自定义字段参数
JS 从零手写实现一个call、apply方法
花的含义
如何用Golang来手写一个Blog - Milu.blog 开发总结
Apache APISIX Ingress v1.5-rc1 released
训练集Loss收敛,但是测试集Loss震荡的厉害?
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
我要抓狂了。。又回到了几天不能A一道题的时候
不是吧?还有人不会定位线上MySQL慢查询问题?
Machine Learning - Ensemble Learning
Official release 2022 Nanjing Zhibo Expo is scheduled to be held in Xinzhuang National Exhibition in October
shell编程流程控制练习
Machine Learning - Logistic Regression