s
[
i
]
=
a
[
1
]
+
a
[
2
]
+
.
.
.
+
a
[
i
]
s[i] = a[1] + a[2] + ... + a[i]
s[i]=a[1]+a[2]+...+a[i]
这样可以直接求出区间
[
l
,
r
]
[l, r]
[l,r]的和
s
[
r
]
−
s
[
l
−
1
]
s[r] - s[l - 1]
s[r]−s[l−1]
#include <iostream>
using namespace std;
int n, m, s[100005];
int main() {
scanf("%d%d", &n, &m);
int a;
for (int i = 1; i <= n; i ++ ) {
scanf("%d", &a);
s[i] += s[i - 1] + a;
}
int l, r;
for (int i = 1; i <= m; i ++ ) {
scanf("%d%d", &l, &r);
printf("%d\n", s[r] - s[l - 1]);
}
return 0;
}