795.一维前缀和
作者:
-whynot
,
2024-08-13 12:38:29
,
所有人可见
,
阅读 1
#include <iostream>
using namespace std;
const int N = 100010;
int a[N];//保存原数组
int s[N];//保存前缀和
int main(){
int n, m;
cin >> n >> m;
//从a[1]开始保存。a[0]是0
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
s[0] = 0;
//计算前缀和
for(int i = 1; i <= n; ++i)
s[i] = s[i - 1] + a[i];
for(int i = 0; i < m; ++i){
int l, r;//保存区间
cin >> l >> r;
//s[r] = a[1]+ ··· + a[r]
//s[l - 1] = a[1]+ ··· + a[l - 1]
//s[r] - s[l - 1] = a[l] + ··· + a[r]
cout << s[r] - s[l - 1] << endl;
}
}