题解 | 【模板】静态区间和(前缀和)
#include <iostream>
using namespace std;
int main() {
int n, q, l, r, tmp;
cin >> n >> q;
auto* a = (long int*)malloc(sizeof(long int)*(n+1));//数组a存储前n个元素之和
a[0] = 0;
for(int i = 1; i <= n; i++){
cin >> tmp;
a[i] = a[i-1]+tmp;
}
for(int i = 0; i < q; i++){
cin >> l >> r;
cout << a[r] - a[l-1] << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")