C++ 代码
// 前缀和的公式 s[i] = a[1]+...a[i]; a[i]=s[i]-s[i-1], s[i]=s[i-1]+a[i]; s[0]=0; a[1]=s[1]-s[0]=s[1]=a[1]
#include <iostream>
using namespace std;
const int N = 1e5+10;
int s[N];
int main() {
int n, m;
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> s[i]; // 就是 a[i]
s[i] = s[i-1] + s[i]; // s[i] = s[i-i] + a[i]
}
int l, r;
while (m --) {
cin >> l >> r;
cout << s[r]-s[l-1] << endl;
}
return 0;
}