原题链接:https://www.acwing.com/problem/content/797/
简单题,只需注意区间的定义,是左闭右开的即可。
#include <iostream>
#define N 100005
int preSum[N]; int ans[N];
int main() { int n, m; preSum[0] = 0; scanf("%d %d", &n, &m); int tmp; for (int i = 0; i < n; ++i) { scanf("%d", &tmp); preSum[i + 1] += preSum[i] + tmp; } int l, r; for (int i = 0; i < m; ++i) { scanf("%d %d", &l, &r); ans[i] = preSum[r] - preSum[l-1]; } for (int i = 0; i < m; ++i) { printf("%d\n", ans[i]); } return 0; }
|