这个还是需要考对题目的理解,然后推导数学公式,得到s[i] = s[i-1] + a[i] sum[l,r] = s[r] - s[l-1]
暴力法应该可以解决,但是会超时
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int a[N]; //原数组
int s[N]; //表示前缀和数组
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
while (m--) {
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", s[r] - s[l - 1]);
}
return 0;
}