刷题记录:牛客NC208910区间求和
记录 刷题 区间 求和 牛客
2023-09-14 09:12:54 时间
传送门:牛客
给定a1……an一共N个整数,有M次查询,每次需要查询区间【L,R】的和。
输入:
5 3
1 2 3 4 5
1 3
3 3
4 5
输出:
6
3
9
一道很明显的前缀和的板题,思维不难直接参考带码即能看懂,用一个数组记录前n项的和即可,答案就是两个区间的前缀和相减啦
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
int sum[maxn];
int main() {
int N,M;N=read(),M=read();
memset(sum,0,sizeof(sum));
for(int i=1;i<=N;i++) {
sum[i]=read();
sum[i]=sum[i]+sum[i-1];
}
int l,r;
for(int i=1;i<=M;i++) {
l=read();r=read();
printf("%d\n",sum[r]-sum[l-1]);
}
return 0;
}
相关文章
- LeetCode刷题记录
- 【错误记录】Android Studio 向 GitHub 提交代码报错 ( Push failed: Failed with error: Could not read from remote )
- MySQL关闭日志记录:走向简洁与高效(mysql关闭日志)
- Linux命令查看DNS记录(linux显示dns)
- 记录一次php占用系统资源过高的问题
- Oracle记录:究竟有多大?(oracle 记录 大小)
- 基于MSSQL的10条记录选取(选10条记录mssql)
- 记录使用Redis记录用户操作历史记录(使用redis存用户操作)
- access下如何恢复已经删除的记录;如何恢复已经删除的表、窗体等等对象
- js简单实现删除记录时的提示效果