题目描述
s[0] = 0
s[1] = a1
...
s[i] = a1 + a2 + ... + ai
=> al + ... + ar = s[r] - s[l - 1]
前缀和求长度为k的子段长度
样例
#include<iostream>
using namespace std;
const int N = 100010;
int s[N];
int n, m;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++){
int x;
cin >> x;
s[i] = s[i - 1] + x;
}
for (int i = 0; i < m; i++){
int l, r;
cin >> l >> r;
cout << s[r] - s[l - 1] << endl;
}
return 0;
}