Files
python/TangDou/AcWing/PrefixAndDifference/795.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

46 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##[$AcWing$ $795$ 前缀和](https://www.acwing.com/problem/content/797/)
### 一、题目描述
输入一个长度为 $n$ 的整数序列。
接下来再输入 $m$ 个询问,每个询问输入一对 $l,r$。
对于每个询问,输出原序列中从第 $l$ 个数到第 $r$ 个数的和。
**输入格式**
第一行包含两个整数 $n$ 和 $m$。
第二行包含 $n$ 个整数,表示整数数列。
接下来 $m$ 行,每行包含两个整数 $l$ 和 $r$,表示一个询问的区间范围。
**输出格式**
共 $m$ 行,每行输出一个询问的结果。
**数据范围**
$1≤l≤r≤n,1≤n,m≤100000,−1000≤数列中元素的值≤1000$
### 二、实现代码
```cpp {.line-numbers}
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int q[N];
int s[N];
// 一维前缀和
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> q[i];
s[i] = s[i - 1] + q[i];
}
while (m--) {
int l, r;
cin >> l >> r;
printf("%d\n", s[r] - s[l - 1]);
}
return 0;
}
```